Getting Data from the DataGrid.

T

tshad

I may be wrong here (and probably am) but from what I have been reading when
when I look at the data from the DataGrid, I am actually seeing what is in
the DataGrid. But I guess I am wrong here.

I have a Table that has a list of emails in it and a DataGrid that displays
some of the columns. I allow the user to select the email they want by
selecting the row. I then take the data out of DataTable and not the
DataGrid at the CurrentRowIndex.

This works fine unless I allow them to sort the DataGrid. The the rows are
not in the same order so the CurrentRowIndex in the DataGrid is not the same
as the row in the DataTable.

Here is the code I am using:
*******************************************************************************
Private Sub EmailDataGrid_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
EmailDataGrid.MouseUp
Dim strInput As String

Dim numRows As Integer =
EmailDataGrid.BindingContext(EmailDataGrid.DataSource, _
EmailDataGrid.DataMember).Count

strInput =
ds.Tables("Emails").Rows(EmailDataGrid.CurrentRowIndex)("id")

Call GetSelectedEmail(EmailDataGrid.CurrentRowIndex)
End Sub

Private Sub GetSelectedEmail(ByVal row As Integer)
EmailFrom.Text = ds.Tables("Emails").Rows(row)("from_address")
EmailTo.Text = ds.Tables("Emails").Rows(row)("to_addresses")
CC.Text = ds.Tables("Emails").Rows(row)("cc_addresses")
BCC.Text = ds.Tables("Emails").Rows(row)("bcc_addresses")
Subject.Text = ds.Tables("Emails").Rows(row)("subject")
Body.Text = ds.Tables("Emails").Rows(row)("body")
TabPage1.SendToBack()
End Sub

*******************************************************************************

How can I change this to look at the correct row?

Or how can I get the Data from the DataGrid itself. This might be a problem
as I would need to put all the data in the DataGrid and I can't seem to tell
the DataGrid to hide the data (as I can in asp.net).

I tried doing in the GridColumnStyles, but that didn't seem to work:

With colStyle4
.MappingName = "Status"
.HeaderText = "Status"
.Width = 50
.Alignment = HorizontalAlignment.Left
.TextBox.Visible = False
End With

The Status column still displayed.

Thanks,

Tom
 
G

Guest

The grid does not actually bind directly to the table (as you have found out)
- it binds to the tables default DataView. This represents the
sorted/filtered view. So you need to do something like this...

dim dv as DatView = ds.Tables("Emails").DefaultView
strinput=dv.Item(EmailDataGrid.CurrentRowIndex).Item("id").ToString

this is untested and may be slightly different depending on the verion of VS
(2003 vs. 2005) but I am not positive.
 
T

tshad

Terry said:
The grid does not actually bind directly to the table (as you have found
out)
- it binds to the tables default DataView. This represents the
sorted/filtered view. So you need to do something like this...

dim dv as DatView = ds.Tables("Emails").DefaultView
strinput=dv.Item(EmailDataGrid.CurrentRowIndex).Item("id").ToString

This works fine and did the job.

So when the DataGrid changes (by sorting from the headers), the
DefaultView also changes?

Thanks,

Tom
 
G

Guest

"So when the DataGrid changes (by sorting from the headers), the DefaultView
also changes?"

The DataGrid is actually using the DataView. So, when you select a column
to sort by, the DataGrid calls the sort on the DataView. You can actually
bind the datagrid to a dataview. In one of my apps., I maintain a collection
of DataViews for a DataTable and when the user selects a column to sort by,
I look to see if I already have that 'view', if yes, I rebind the grid to
that view, if not I create a new DataView, add it to the collection and bind
it to the grid. That way, as the user switches the sorted column, I never
perform the sort more then once and only if I need it.
 

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