0

How to add a row in datagridview in winform application.

Scenario is:

  • I've one grid on windows form.
  • Grid's columns are pre-definded. (Here, I mean column index, name , header text and order is not changeable at runtime.)
  • I need to add new row in that grid.

Anyone, help me in this regard. How can I do this task ?

2
  • datagridview.datasource= yourDataTabl; Add new row into datatable and then bind again or add programaitcally to gridview Commented Jul 11, 2012 at 7:04
  • What about if you've no data source ? I mean, just want to add rows in grid and save them on application close or form close that contains grid. Commented Jul 11, 2012 at 7:07

5 Answers 5

2

Just try like this -

dataGridView1.Rows.Add(<col1 value>,<col2 value>,...)

The parameters will be values you intended for your each columns.

EDIT :

Taking your code from other comments, you can simply use it like this-

myMainApplication.dgvBooksDetails.Rows.Add(objBook.ID, objBook.Title, objBook.Author, 
            objBook.Genre, objBook.Price, objBook.PublishDate, objBook.Description);

EDIT

In Sub Form, store instance of Main Form -

public MainForm _mainForm;

In Main Form :

SubForm frm = new SubForm();
frm.MainForm = this;
frm.Show();

Then to fill up datagrid, do like this -

_mainForm.dgvBookDetails.Rows.Add(.....

Hope it helps!

Sign up to request clarification or add additional context in comments.

9 Comments

col1 mean column name or id ?
its column value. if your column is of type DataGridViewTextBoxColumn and your value is "Hello World", the syntax would be `dataGridView1.Rows.Add("Hello World")...
Actually it depends on data gridview column type. You need to check all your datagrid column types and pass parameters in Add() function accordingly.
All the columns are textboxes. So, what to do next ?
Well, I come to conclusion and it is that: if I code in main application then it add rows. But if I open sub form and set values of row and add. It doesn't update grid on main form. Do you've any idea about this matter ?
|
0

Simply, add the row in your datatable and bind the source with your datagridview like this:

row = dtTable.NewRow();
///HereEnter the value in row
dtTable.Rows.Add(row);
dataGridView1.DataSource = dtTable;

If you want to add row without using datasoruce, then you have to use DataGridViewRowCollection.Add method.:

Comments

0

Add newrow in your datatable and bind it as i mentioned in my above comments or do this way

 private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // Get all rows entered on each press of Enter.
            var collection = this.dataGridView1.Rows;
            string output = "";
            foreach (DataGridViewRow row in collection)
            {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if (cell.Value != null)
                {
                output += cell.Value.ToString() + " ";
                }
            }
            }
            // Display.
            this.Text = output;
        }

Comments

0

Doesn't dataGridView1.Rows.Add(1) do the trick for you ?

It should add a row corresponding to RowTemplate of the DataGridView

Comments

0

If you are using a DataTable object as the datasource for your DatagGridView then i suggest you use a binding source.

BindingSource bs = new BindingSource();

bs.DataSource = DataTable;

DataGridView1.DataSource = bs;

Now add rows to the Data Table as mentioned previously.

Using this method, any changes that are made to the Data Table are automatically reflected in your GridView.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.