Reputation: 33
I have used DataGridview, which is supposed to be populated manually (No data sources specified). I want to get its content of first row(index-0). But it gives me null cell values.
Code
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Description");
dt.Columns.Add("Unit Price");
dt.Columns.Add("Quantity");
dt.Columns.Add("Tot");
dr = dt.NewRow();
dr[0] = dataGridViewA_General.Rows[0].Cells[1].Value; //these values are null
dr[1] = dataGridViewA_General.Rows[0].Cells[2].Value; //these values are null
dr[2] = dataGridViewA_General.Rows[0].Cells[3].Value; //these values are null
dr[3] = dataGridViewA_General.Rows[0].Cells[4].Value; //these values are null
dt.Rows.Add(dr);
Thanks in advance
Updated
// dataGridViewA_General
this.dataGridViewA_General.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewA_General.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column_A_item_code,
this.Column_A_description,
this.Column_A_qntity,
this.Column_A_unit_price,
this.Column_A_list_total});
// Column_A_description
this.Column_A_description.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.Column_A_description.HeaderText = "Description";
this.Column_A_description.Name = "Column_A_description";
// Instantiate
private System.Windows.Forms.DataGridView dataGridViewA_General;
private System.Windows.Forms.DataGridViewTextBoxColumn Column_A_description;
(rest of the columns are also defined in the sameway.)
Upvotes: 0
Views: 1407
Reputation: 33
I had done done a logical mistake and code had not been executed in expected manner after filling data. Actually the above code is correct. Thank you all for your valuable contribution.
Upvotes: 0
Reputation: 25056
You must add the row to the DataTable.
dt.Rows.Add(dr);
NewRow(); simply creates a blank row, you must add it manually.
Also, as you didn't put anything in the row itself, nothing will be returned.
Upvotes: 3