4
Reply

How to Clear DataGridView Rows?

divya

divya

Jul 13 2012 8:23 AM
1.2k

Hai Guys..

    In My application for a gridview Some rows are binded  from database and remaining rows are added statically by using the below given code..

   DataGridViewColumn col2 = new DataGridViewColumn();
                    DataGridViewCell cell2 = new DataGridViewTextBoxCell();
                    col2.CellTemplate = cell2;
                    col2.HeaderText = "No of Days";
                    col2.Name = "No of Days";
                    col2.Visible = true;
                    col2.Width = 40;
                    if (dgvEmployeeSalary.Columns.Contains("No of Days") == true)
                    {
                    }
                    else
                    {
                        dgvEmployeeSalary.Columns.Add(col2);

                    }
                    //dgvEmployeeSalary.Columns.Add(col2);
                    foreach (DataGridViewRow row in dgvEmployeeSalary.Rows)
                    {
                        if (row.Cells["Employee ID"].Value != null)
                        {
                            row.Cells["No of Days"].Value = txtNoofWorkingDays.Text;
                        }
                    }

          I tried   dgvEmployeeSalary.datasource=null
 its working but while trying to rebind data gridview ,then column index is changing..
can any one help me...

Answers (4)