Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnSave_Click(object sender, EventArgs e)
       {
           int i = 0;
           foreach (DataGridViewRow row in dataGridView1.Rows)
           //{

           {
               string keyword = row.Cells[0].Value.ToString();
               string name = row.Cells[1].Value.ToString();
               string comm = row.Cells[2].Value.ToString();
               string retur = row.Cells[3].Value.ToString();
               string message = row.Cells[4].Value.ToString();
               i = pbl.fsave(keyword, name, comm, retur, message);
           }
               //string insert_sql = "INSERT INTO TblProtocolDetails(KeyWord,Command,Return)VALUES('" + keyword + "','" + comm + "','" + retur + "')";

           //}
       }
Posted

1 solution

Hello ,

I think one of the Datagridview property AllowUserToAddRows is True . Hence when you add rows in your datagridview , a blank row added . Now , when you save the datagridview in your database , the last row obviously null .

I suggest you to write a For loop instead of Foreach . like this way ..

for(int i=0;i<datagridview1.Rows.Count-1 ; i++ )
{
//others stuffs .....

}


thanks
animesh
 
Share this answer
 
v2

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