[英]how to disable empty cells in datagridview clickable in c#
This is my code so that every time the user clicks on a row it goes to a textbox
, my problem is when I click an empty cell there is an error. 这是我的代码,因此,每当用户单击行时,都会转到
textbox
,我的问题是,当我单击一个空单元格时会出现错误。 How can I disable clickability of the empty cells? 如何禁用空白单元格的可点击性?
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txt_voterid.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
txt_fname.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
txt_lname.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
txt_age.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
txt_vstatus.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
txt_uname.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
txt_pword.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
}
replace this line 替换这条线
txt_voterid.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
with this 有了这个
if(!string.IsNullOrEmpty(Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value)))
txt_voterid.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.