Getting Data from the DataGrid.

Getting Data from the DataGrid.

Post by tsha » Thu, 08 Nov 2007 03:17:07


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
 
 
 

Getting Data from the DataGrid.

Post by tsha » Thu, 08 Nov 2007 10:12:38

Terry" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

This works fine and did the job.

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

Thanks,

Tom



 
 
 

Getting Data from the DataGrid.

Post by VGVycn » Thu, 08 Nov 2007 22:34:00

"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.


--
Terry