这是我写的一个函数,调用事件textBox3_TextChanged
我对Windows.Forms下的dataGrid1 太不熟悉了
请教一下各位高手,dataGrid1 怎么样设置才可以默认选重第一行
当我双击第一行时,则把第一行的其中一个字段的值(事先指定好其中的一个字段),写到textBox3.Text.谢谢高手救命,分不够给我说 加就是了
下面是我写的函数,可以查出我想要的东西,具体怎么改请高手指点一下
分不够可以加
public void FuWuSheng()
{
string strConn = " Initial Catalog=SYXT;Data Source = LEIKE;User ID=sa;Password=sa"; SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConn;
try
{
SqlCommand selcmd = new SqlCommand();
selcmd.Connection = conn;
selcmd.CommandText = "select * from GuYuan where GYID like '" + textBox3.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selcmd;
DataSet ds = new DataSet();
da.Fill(ds,"GuYuan");
dataGrid1.DataSource = ds.Tables["GuYuan"];
conn.Open ();
conn.Close();
}
catch (Exception a)
{
MessageBox.Show (a.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
}
我对Windows.Forms下的dataGrid1 太不熟悉了
请教一下各位高手,dataGrid1 怎么样设置才可以默认选重第一行
当我双击第一行时,则把第一行的其中一个字段的值(事先指定好其中的一个字段),写到textBox3.Text.谢谢高手救命,分不够给我说 加就是了
下面是我写的函数,可以查出我想要的东西,具体怎么改请高手指点一下
分不够可以加
public void FuWuSheng()
{
string strConn = " Initial Catalog=SYXT;Data Source = LEIKE;User ID=sa;Password=sa"; SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConn;
try
{
SqlCommand selcmd = new SqlCommand();
selcmd.Connection = conn;
selcmd.CommandText = "select * from GuYuan where GYID like '" + textBox3.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selcmd;
DataSet ds = new DataSet();
da.Fill(ds,"GuYuan");
dataGrid1.DataSource = ds.Tables["GuYuan"];
conn.Open ();
conn.Close();
}
catch (Exception a)
{
MessageBox.Show (a.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
}
定义在datagrid的mouse up 或down事件
i = datagrid.CurrentRowIndex
Me.textbox3.Text = datagrid.Item(i, 0)
Me.textbox4.Text = datagrid.Item(i, 1)这样 datagrid的第一行的第一列值就与textbox3.text属性绑定上了
textBox1.Text = dataT.Rows[dataGrid1.CurrentCell.RowNumber][0].ToString();
textBox3.Text = dataGrid1.CurrentCell[dataGrid1.CurrentCell.RowNumber,0].ToString();
{
i = dataGrid1.CurrentCell.RowNumber;
dataGrid1.Select(i);
}
声明一个全局变量 int i = 0; 等于0则默认选种第一行
当你选择行时,利用dataGrid1.CurrentCell.RowNumber当前的行数
执行选择dataGrid1.Select(i);可以解决你的选择行的要求至于你的第二个问题:当我双击第一行时,则把第一行的其中一个字段的值(事先指定好其中的一个字段),写到textBox3.Text这个问题,我只能做到当你点击第一行时,的根据你点的哪个单元格的值传递给textBox3.Text所以达不到你的要求,我非常抱歉。
http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
如果非要取值,
取出DataGrid当前行对应的DataTable的值。
如果DataGrid绑定的是DataView,
DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
//如果绑定的是DataTable
//DataRow dw = (DataRow)this.BindingContext[this.dataGrid1.DataSource,this.dataGrid1.Memeber].Current;
获取到drv就可以改数据,
DataRow dw = drv.Row; // 得到DataRow,
DataTable dt = dw.Table;//得到DataTable
DataSet ds = dt.DataSet;
private void button1_Click(object sender, System.EventArgs e)
{
FuWuSheng();
this.dataGrid1.Select(0);
} private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.textBox1.Text = ds.Tables[0].Rows[dataGrid1.CurrentCell.RowNumber][dataGrid1.CurrentCell.ColumnNumber].ToString();
}
public void FuWuSheng()
{
string strConn = "Persist Security Info=False;Integrated Security=SSPI;database=pubs;server=epaib"; SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConn;
try
{
SqlCommand selcmd = new SqlCommand();
selcmd.Connection = conn;
selcmd.CommandText = "select * from authors ";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selcmd;
ds = new DataSet();
da.Fill(ds,"authors");
dataGrid1.DataSource = ds.Tables["authors"];
conn.Open ();
conn.Close();
}
catch (Exception a)
{
MessageBox.Show (a.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
}
我已经测试成功.....不知道是不是楼主要的结果
dataGrid.HitTest(row, col);剩下的我想你就明白了吧!
正行选中要在MouseUp事件里写dataGrid.Select(dataGrid.HitTest(e.x,e.y).Row);