简体   繁体   中英

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM