0

I'm working on a datagridview to store the user data in database(sql server). The question is how to find the new row in datagridview in c# ?

For example: if the user already entered 100 rows data . While he open the application he can find the new row first column by clicking new button . So that the cursor will be in 101's row first column to begin edit.

Can any one give me the sample coding for that pls....

leppie
  • 115,091
  • 17
  • 196
  • 297
kalai
  • 25
  • 1
  • 8
  • How you get data to datagridview? Set a `.DataSource` property of DataGridView or manually adding a rows with data? – Fabio May 01 '13 at 11:17

1 Answers1

0

This code shows how you can add the row, select it and begin the edit in the first cell:

DataGridView dgv = new DataGridView();
dgv.Rows.Add();
dgv.Rows[dgv.Rows.Count - 1].Cells[0].Selected = true;
dgv.BeginEdit(false);

So in the Button Click Event Handler you can put this:

private void button1_Click(object sender, EventArgs e)
{
    dgv.Rows.Add();
    dgv.Rows[dgv.Rows.Count - 1].Cells[0].Selected = true;
    dgv.BeginEdit(false);
}
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
  • affraid this will not work when datagridview is a data-bound. Check this link: [Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound](http://stackoverflow.com/questions/11445121/rows-cannot-be-programmatically-added-to-the-datagridviews-rows-collection-when) – Fabio May 01 '13 at 11:19
  • Yes,in that case he should remove dgv.Rows.Add() statement. – Nikola Davidovic May 01 '13 at 11:34
  • Yes and add new row with default values in DataSourceused for data-bound (DataTable for example).... – Fabio May 01 '13 at 11:58
  • After using ur code it shows error message as "Operation cannot be performed because there is no current cell. The CurrentCell property needs to be set first." – kalai May 02 '13 at 04:20