DataGridView C# Default Values in Rows During Edit

2019-05-22 20:20发布

问题:

I have a program that I use to enter values into a database using a gridview. The gridview is populated with an 'empty' result from a DataSet that queries that database from the table I want. This fills the grid with the proper columns that I want (ie. a grid with an empty row from a certain table in a db). When you fill in the columns, I take the data and manually update the db.

Basic example of how I use the gridview:

this.fullUutDataTableAdapter.Fill(this.dalsaUutDataSet.ResultsFullUut);  
DataView dv = this.dalsaUutDataSet.ResultsFullUut.DefaultView;
Grid_modify.DataSource = dv;            
foreach(DataGridViewRow dr in Grid_modify.Rows)
{
    dr.Cells["YieldID"].Value = -1;
}

However, I want to have certain columns to have Default Values. I have tried changing the column's default value, but you cannot change the property, only 'GET' it. Alternatively, I tried parsing through the rows in the empty grid and filling in the values. This works fine when the grid loads up initially (values show up in proper places). HOWEVER, when as soon as I enter edit mode these values clear to nothing.

NEXT, I tried using some of the RowEdit Events and tried filling in values when edit mode is entered, however, this causes problems when you enter and leave edit mode. Seems to be a more complicated and messy way of doing things that seemingly necessary.

So the question really is, what is the best way to fill a gridview with default values in certain columns?

I would prefer the "cleanest" solution possible. However, I recognize that things don't always work out that way.....

Thanks for your help in advance! Cheers.

回答1:

Take a look at this: How To Specify Default Values for DataGridView