Groups | Blog | Home
all groups > vb.net > january 2008 >

vb.net : How to set values in datagrid?


Michael
1/28/2008 7:18:03 AM
Hi all,
I am using Datagrid control in VB.Net 2003.
Now I have a problem like this,
when user changed the value in a cell in datagrid, I want all the
values in the same column below the current row changed to the new
value.
How can I do this?
I use currentcellchaged event like below:

Private Sub myGRID_CurrentCellChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles myGRID.CurrentCellChanged
''''Get total row count
Dim i as Integer
Dim NewValue as Integer
NewValue=CInt(myGRID.Item(myGRID.CurrentRowIndex,
ColumnNumber))
i = myGRID.BindingContext(Me.ALLOCATIONS_GRID.DataSource,
myGRID.DataMember).Count()
If (myGRID.CurrentRowIndex + 1) < i Then
myGRID.Item(myGRID.CurrentRowIndex + 1, ColumnNumber) =
NewValue
End If
End Sub


Can anybody advise on this?
I always get "ListManager's position must equal to rownum" exception.
Thanks.


Regars,
Michael
Cor Ligthert[MVP]
1/28/2008 6:16:56 PM
Michael,


A datagrid is a data control.

To access it you can the most simple use the object in the datasource (a
dataview or a datatable).

That is simple to do with a for each or a for index loop.


Cor
Michael
1/28/2008 10:15:31 PM
On Jan 29, 1:16=A0am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
[quoted text, click to view]

Thanks Cor,
Would you please tell me more if I do this in the datatable?
What event should I put the code in?
Thanks.

Regards,
Cor Ligthert[MVP]
1/29/2008 7:30:00 PM
Michael,

Assuming that your datasource is a datatable.

\\\
dim dt as DataTable = DirectCast(TheDataGrid.DataSource, DataGrid)
for each dr as DataRow in dt.Rows
dr.Item("TheField") = "Michael"
next
///

Cor Ligthert[MVP]
1/29/2008 8:03:18 PM
Sorry,
[quoted text, click to view]
dim dt as DataTable = DirectCast(TheDataGrid.DataSource, DataTable)
Michael
1/29/2008 10:42:19 PM
On Jan 30, 3:03=A0am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
[quoted text, click to view]

Hi Cor,
thanks for your help.
But what I want is like this,
I have a datatable, and use a datagrid to display it.
users want to modify the value in datagrid,
let's say he changed the value in row 1, column 1,
now I have to changes all the values in coumn 1 for the other rows.
I guess I should make this change in some datagrid event, because this
should be invoked automatically.
That is, after users modify the value in a cell, datagrid will
automatically change the value in below rows.
Hope this make sense.
Thanks.

Regards,
Cor Ligthert [MVP]
1/30/2008 12:45:19 PM
Michael,

See this sample on our pages.
http://www.vb-tips.com/DataGridRowCount.aspx

This gives you the count, however you can as well get the position and set
it than as starting index in a for index loop instead of the for each loop
that I have showed you,

Cor

AddThis Social Bookmark Button