Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a gridview and a button on winForm. i want to save data from datagrid to a table but i want to ensure that user have filled all columns of gv. how can i check it on button click event??
Posted

1 solution

Hi, i think a better apporch is to check that the user have filled the data correctly in the begining.

To check if the datagridview contains valid data you may use somthing like this:


C#
foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[1].Value.toString()) == "") //depends on your not filled cell                                                           //logic 
                    {

//the cell is empty, you may in your case check if the value is null
                        
                    }
                }


I am totaly against using this kind of solution. the best way is to create a class and then affect the class to the datagrid.
your will then work only with lists. much easier, faster and efficient.

C#
//you will have to iterate throw the elements of your list.
Foreach(User user in userList)
{
if(user.Name == null || user.Name == "")
   {
      //the user Name is not filled
   }
}

Hope you will consider using the second solution.



Hope it helps.
 
Share this answer
 
Comments
Muhamad Faizan Khan 19-May-14 8:33am    
object refrence not set to an instance of an object
Ziee-M 19-May-14 8:59am    
it seems you are not doing enought effort! you are not supposed to copy paste a code espacially when you are not puting some of yours into the questions! try work harder, put menangful comments, not a "object refrence not set to an instance of an object".
Thanks7872 19-May-14 8:59am    
This doesnt make sense. You have to mention at which line.
Muhamad Faizan Khan 7-Jun-14 6:58am    
i enter blank data here in cell and cheked this condition
if (row.Cells[1].Value.toString()) != "") //
then here error occur object refrence not set to an instance of an object

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