Programmatically assigning a value to a DataGridCell

  • Thread starter Thread starter Moshe Lupiansky via .NET 247
  • Start date Start date
M

Moshe Lupiansky via .NET 247

(Type your message here)
1 - How do I programmatically assign a value to a specified cell in a DataGrid? Something like DataGrid1(3,8) = "ABC"

2 - When I am going to the last row (the empty one)in order to insert a new record into the DB, I want the default values of the fields that do have default values to be displayed when I first enter this row. How do I achieve it?
 
1) myDataGrid.CurrentCell = New
DataGridCell(RowIndexYouWant,ColumnIndexYouWant);

2) Have you set default values for the underlying DataColums of the
DataTable? If so, they should be populating themselves when a new row is
added. For instance,
myDataSet.Tables["SomeTable"].Columns[SomeColumnIndex].DefaultValue =
"Whatever"; //In this http://www.knowdotnet.com/articles/types.html Article
I'm discussing types but the example I use is regarding defaultvalues of
DataColumns which is what you'd need.

"Whatever should appear when you add a new row or if you're using a
BindingContext, when you call .AddNew

You may also want to set the Nulltext property of your DataGridColumnStyle
if you want those values to 'appear' in the grid but not actually be there.
If you use Defaults, then "Whatever" will actually get sent back to the db
(using the above example) as SomeColumnIndex in the new row when you call
Update. If you just wanted it to say "Whatever" but not be delivered back
to the DB, then use NullText instead of default value.

HTH,

Bill

HTH,

Bill

Moshe Lupiansky via .NET 247 said:
(Type your message here)
1 - How do I programmatically assign a value to a specified cell in a
DataGrid? Something like DataGrid1(3,8) = "ABC"
2 - When I am going to the last row (the empty one)in order to insert a
new record into the DB, I want the default values of the fields that do have
default values to be displayed when I first enter this row. How do I achieve
it?
 
In response to question one

datagrid1[0,0] = someValue

you could also do it to the datasource if you've databound it

DataView dv = new DataView(tableIMadeEarlier)
datagrid1.DataSource = dv

dv.Table.Row[datagrid1.CurrentCell.RowNumber][datagrid.CurrentCell.ColumnNumber] = someValue

jax
 

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

Back
Top