By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Adding data by column name to DataGridView in C#

P: 12
I've added a DataGridView and added some columns in the designer, now I want to add some rows with data applied via column name.

Having done some web trawling I've ended up with this:
Expand|Select|Wrap|Line Numbers
  1. private void AddRow(string thing,string thingdesc)
  2. {
  3.             DataGridViewRow row = new DataGridViewRow();
  4.             row.CreateCells(MyGrid);     // #1
  5.             row.Cells[0].Value = "Add";  // #2 this makes it work
  6.             MyGrid.Rows.Add(row);       // #3 need this before adding data
  7.             row.Cells["Selected"].Value = "Add";
  8.             row.Cells["Model"].Value = thing;
  9.             row.Cells["Description"].Value = thingdesc;
  10.             row.Cells["Scenario"].Value = "Basic";
  11.             row.Cells["ModelLoop"].Value = "1";
  12.             row.Cells["LaunchCount"].Value = "1";
  13. }
  14.  
Apparently I need lines marked # above for it to work. Without #2 the lines after #3 come back with System.ArgumentOutOfRangeException. It seems that without #2, CreateCells() doesn't.

Also the data has to be added after #3. Putting it between #2 & #3 gives e.g. Column named Selected cannot be found.

I would have thought #1 was sufficient to allow me to add the data.

Any thoughts?

Simon
Nov 2 '07 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
There is a slightly quicker way, "MyGrid" is your datagridview?
Expand|Select|Wrap|Line Numbers
  1. private void AddRow(string thing,string thingdesc)
  2. {
  3.    int idx = MyGrid.Rows.Add();
  4.    DataGridViewRow row = MyGrid.Rows[idx];
  5.    row.Cells["Selected"].Value = "Add";
  6.    row.Cells["Model"].Value = thing;
  7.    row.Cells["Description"].Value = thingdesc;
  8.    row.Cells["Scenario"].Value = "Basic";
  9.    row.Cells["ModelLoop"].Value = "1";
  10.    row.Cells["LaunchCount"].Value = "1";
  11. }
  12.  
Nov 2 '07 #2

P: 12
Excellent! Works a treat.

Many thanks.
Nov 6 '07 #3

P: 1
I've added a DataGridView and added some columns in the designer, now I want to add some rows with data applied via column name.

Having done some web trawling I've ended up with this:
Expand|Select|Wrap|Line Numbers
  1. private void AddRow(string thing,string thingdesc)
  2. {
  3.             DataGridViewRow row = new DataGridViewRow();
  4.             row.CreateCells(MyGrid);     // #1
  5.             row.Cells[0].Value = "Add";  // #2 this makes it work
  6.             MyGrid.Rows.Add(row);       // #3 need this before adding data
  7.             row.Cells["Selected"].Value = "Add";
  8.             row.Cells["Model"].Value = thing;
  9.             row.Cells["Description"].Value = thingdesc;
  10.             row.Cells["Scenario"].Value = "Basic";
  11.             row.Cells["ModelLoop"].Value = "1";
  12.             row.Cells["LaunchCount"].Value = "1";
  13. }
  14.  
Apparently I need lines marked # above for it to work. Without #2 the lines after #3 come back with System.ArgumentOutOfRangeException. It seems that without #2, CreateCells() doesn't.

Also the data has to be added after #3. Putting it between #2 & #3 gives e.g. Column named Selected cannot be found.

I would have thought #1 was sufficient to allow me to add the data.

Any thoughts?

Simon

HEllo
DO One Thing
private void AddRow(string thing,string thingdesc)
{
string [] row1 = { comumn1(Value), comumn2(Value),........}
string [] row2= { comumn1(Value), comumn2(Value),.........}

datatgridView1.add(row1);
datatgridView1.add(row2);

}


Thats all
Hope This will help You

Regards
Ramesh
Nov 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.