Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Adding default cell values
 

Adding default cell values

When doing an addnew(), I would like to add some default values to a few fields.

Suggestions?

greenhouse  Wednesday, December 26, 2007 7:12 PM
greenhouse wrote:

Just as a short aside, I though default values could also be set in the table definition.

I tried that, but nothing happened or I did not do it correctly.



Yes, this can be defined in the table definition, just set Default Value property for the specified field. Something like this

Code Block

void Form1_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("c1");

dt.Columns.Add("c2");

dt.Columns[1].DefaultValue = "default value";

for (int j = 0; j < 5; j++)

{

dt.Rows.Add("aaa", "bbb");

}

bs.DataSource = dt;

this.dataGridView1.DataSource = bs;

}

BindingSource bs = new BindingSource();

private void button1_Click(object sender, EventArgs e)

{

bs.AddNew();

}

Zhi-Xin Ye  Monday, December 31, 2007 7:19 AM
If you are using a datagridview you can handle the DefaultValuesNeeded event or the datatable's TableNewRow event


Ken Tucker  Wednesday, December 26, 2007 8:02 PM

Thanks. I think I'll choose the TableNewRow event.

Just as a short aside, I though default values could also be set in the table definition.

I tried that, but nothing happened or I did not do it correctly.

greenhouse  Wednesday, December 26, 2007 8:27 PM
greenhouse wrote:

Just as a short aside, I though default values could also be set in the table definition.

I tried that, but nothing happened or I did not do it correctly.



Yes, this can be defined in the table definition, just set Default Value property for the specified field. Something like this

Code Block

void Form1_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("c1");

dt.Columns.Add("c2");

dt.Columns[1].DefaultValue = "default value";

for (int j = 0; j < 5; j++)

{

dt.Rows.Add("aaa", "bbb");

}

bs.DataSource = dt;

this.dataGridView1.DataSource = bs;

}

BindingSource bs = new BindingSource();

private void button1_Click(object sender, EventArgs e)

{

bs.AddNew();

}

Zhi-Xin Ye  Monday, December 31, 2007 7:19 AM

You can use google to search for other answers

Custom Search

More Threads

• binding datagridview to a bindingsource
• Multiple database tables in a single Datagridview
• problem with Combo Boxes, automation error
• DataGridViewComboBoxColumn and AutoCompleteMode, AutoCompleteSource property.....?
• How do I create a GridView/edit screen?
• Databinding and Timer Thread Problem
• Custom business objects and databinding
• Controls not showing in additional form
• Attribute must be unique
• DataAdapter.Update() dosen't work !