Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to check if my datagridview column[3] is empty .
i have four columns and when im calling one column , that isn't working.
My program is when im searching one data from datagridview ,i need to check if any column is empty empty associated with that data

What I have tried:

this is how i set my datagridview..
string[] row = new string[] { };
row = new string[] { Convert.ToString(dataReader[0]), Convert.ToString(dataReader[1]), Convert.ToString(dataReader[2]), Convert.ToString(dataReader[3]
dataGridView1.Rows.Add(row)...
Posted
Updated 27-May-21 10:20am
Comments
Richard MacCutchan 27-May-21 6:15am    
You are assuming that all four columns contain data that can be converted to strings. But what if one column does not contain any data? Check your inputs first, don't assume they all contain valid data.

1 solution

I'm not sure which event you are trying to handle such check. I suggest you do it in the data grid view CellContentClick or CellContentDoubleClick. There you get passed an args that contains the location of the cell to evaluate. The Convert.ToString() is actually the recommended way because it's safe. You can then do something like this

C#
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    var cell = dataGridView1[e.ColumnIndex, e.RowIndex];

    string strVal = Convert.ToString(cell.Value);

    if (string.IsNullOrEmpty(strVal))
        MessageBox.Show("empty");
    else
        MessageBox.Show(strVal);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900