Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have bankname combobox,datagridview,one save button and add button .If Select bankname from the combobox.Available records showing in the datagridview based on the bank selected.
after i click the add button.first i adding new empty row to datatable at 0th position and binding to datagridview.
What i want is bankname is sbi i selected datagridview row is showing like slno=1 startno=100012 like that.If now if i click the add button empty row adding fine but already existed recored changed the slno=2 new row slno=1 this is my problem.How to solve this.Please help me on this.Thanks in Advance.

What I have tried:

C#
private void btnadd_Click(object sender, EventArgs e)
     {
         if (cmbbankname.SelectedIndex != 0)
         {
             dts.Rows.InsertAt(dts.NewRow(),0);
             LoadSerial();
         }
         else
         {
             MessageBox.Show("Select BankName for Adding New Record");
         }
     }
        private void LoadSerial()
      {
         int i = 1;
         foreach (DataGridViewRow row in dgvwChqs.Rows)
         {
             row.Cells["Slno"].Value = i; i++;
         }
     }
Posted
Updated 16-Jun-17 0:17am

1 solution

Hello ,
Why you are trying to insert new row at the top position of the Table by below code
dts.Rows.InsertAt(dts.NewRow(),0)
hence slno changed respectively. Simply add Row by Add method instead of Insert method.

DataRow dr = dts.NewRow();
dr[0] = "coldata1";
dr[1] = "coldata2";
dr[2] = "coldata3";
dr[3] = "coldata4";

dts.Rows.Add(dr);//this will add the row at the end of the datatable

Thanks
 
Share this answer
 
Comments
Member 13153537 16-Jun-17 6:31am    
I want newly Added Slno show on the top of the datagridview.
Animesh Datta 16-Jun-17 6:41am    
For existing record the slno should start from 2 as new record already inserted at top position.
Member 13153537 16-Jun-17 6:50am    
What i want is in my datagridview i had 1 row already .If i add new row will be added at top of the datagridview with slno 2.
Animesh Datta 16-Jun-17 7:08am    
From my opinion , do not call LoadSerial() function for every time when new row adds.Instead , you may call LoadSerial() for first time when record fetched from database and then increase the slno for new record which are inserting dynamically.
Member 13153537 16-Jun-17 7:12am    
Thanks for your response.

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