读书人

请问dataGridView1取值有关问题

发布时间: 2012-09-09 09:27:54 作者: rapoo

请教,dataGridView1取值问题
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

int rowindex = dataGridView1.CurrentCell.RowIndex;

//获得当前行的第一列的值
string value1 = dataGridView1.Rows[rowindex].Cells["qq_id"].Value.ToString();
textBox1.Text = value1;




}



private void button1_Click(object sender, EventArgs e)

{
int rowindex = dataGridView1.CurrentCell.RowIndex;

//获得当前行的第一列的值
string value1 = dataGridView1.Rows[rowindex].Cells["qq_id"].Value.ToString();
textBox1.Text = value1;

}

请教下,为什么点击button1按钮 text可以显示,点击dataGridView1不能显示值

[解决办法]

C# code
 string value1 = dataGridView1.Rows[e.Rowindex].Cells[e.ColunmIndex].Value.ToString();  textBox1.Text = value1;
[解决办法]
if (!DBNull.Value.Equals(DataGridView1.CurrentCell.Value))
{
string str = DataGridView1.CurrentCell.Value.ToString();
textBox1.Text =str;
}

最好是用CurrentCellChanged事件处理

读书人网 >C#

热点推荐