datagridview

C

Claudia Fong

I need to add new rows into a datagridview manually.. but when I first
added the new row I got an error of:
No row can be added to a DataGridView control that does not have
columns. Columns must be added first.

But when I add the column I got this error:

At least one of the DataGridView control's columns has no cell template.

Can someone help me?

I want to add something like this:

Name Id Balance
ABC 123 300


Cheers!

Claudi
 
R

raylopez99

I need to add new rows into a datagridview manually.. but when I first
added the new row I got an error of:
No row can be added to a DataGridView control that does not have
columns. Columns must be added first.

But when I add the column I got this error:

At least one of the DataGridView control's columns has no cell template.

Can someone help me?

I want to add something like this:

Name     Id     Balance
ABC      123     300      

Cheers!

    Claudi

*** Sent via Developersdexhttp://www.developersdex.com***

I don't know what you mean by 'manually'? You mean by hand, or by
code?

If by code or by hand, just start from scratch. Somewhere you have a
self-referential pointer that points back to itself and throws you for
a loop. Stuff is binding to stuff that is also trying to bind and you
have this back and forth situation--pardon my scientific language.

My two cents, for what it's worth.

RL
 
J

jp2msft

Claudia:

First, your DataGridView has to be "unbound."

If that is the case, here is a snippet that might get you going:

foreach (DataRow row in DataSet1.Tables[tableName].Rows) {
DataGridViewRow dgRow = new DataGridViewRow();
dgRow.CreateCells(DataGridView1);
object[] obj = row.ItemArray;
for (int j = 0; j < obj.Length; j++) {
dgRow.Cells[j].Value = string.Format("{0}", obj[j]);
}
DataGridView1.Rows.Add(dgRow);
dgRow = null;
}
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

Hope that helps!
 
C

Ciaran O''Donnell

Try this:

//Add the first column
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Name";
col.ValueType = typeof(String);
dataGridView1.Columns.Add(col);
//Add the second column
col = new DataGridViewTextBoxColumn();
col.HeaderText = "Id";
col.ValueType = typeof(int);
dataGridView1.Columns.Add(col);
//Add the third column
col = new DataGridViewTextBoxColumn();
col.HeaderText = "Id";
col.ValueType = typeof(int);
dataGridView1.Columns.Add(col);

//add the data
dataGridView1.Rows.Add("ABC", 123, 300);

//auto size the columns to make them pretty
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top