Does a DataGrid have a value property?

Hi all, coding C# in VS.NET 2003

I'm databinding a recordset to a datagrid and when the user selects a row in the grid I need to return the primary key value.  What I've been doing is just using the CurrentRowIndex and getting that record from the DataSet.  It's been working fine but then I tried sorting the records and it no longer works because now the row indexes in the grid do not reflect the indexes in the dataset.

I'm wondering if DataGrids have a value property similar to a combo box OR if there is a better way of accomplishing my goal.
alexatsearidgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dstanley9Commented:
There is no Value property.  Best way is to add the primary key as a column in the datagrid.  Or use a DataView to give you a view back into the source datatable.  Then don't sort the dataview.
0
alexatsearidgeAuthor Commented:
The user needs to be able to sort the DataGrid and the primary key must be hidden.  
I format my dataview to hide the first column and then bind it to the grid.
0
dstanley9Commented:
You could add the primary key as a hidden field (<asp:HiddenField value='<%# Eval("keyField")%>'/>) in one of the columns.  You need to have it in the DataGrid in some fashion to know which record you are dealing with.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

alexatsearidgeAuthor Commented:
I don't mind if it is in the grid, as long as it is not visible.
Could you provide a code sample?  Also this is a stand alone dialog based application.  I'm not using ASP.
0
dstanley9Commented:
Sorry I asumed you were in web forms.  The DataGrid will bind to the table's DefaultView be default, so it should pick up the sorting from there.

try using

DataRowView row = dataTable.DefaultView[dataGrid.CurrentRowIndex];

Note that it won't be type-safe (and you can't cast to a type-safe row) but you can get the primary key from there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alexatsearidgeAuthor Commented:
Which brings us back to the original problem.  The grid and the view are not consistent in the sorting.  Do I need to handle when the Grid is sorted in order to sort the DataView?
0
dstanley9Commented:
I guess I didn't say that right.  The DataGrid should use the DefaultView of the data table by default.  So when you sort the DataGrid, the DefaultView should be sorted as well and should match the DataGrid.  So you can get the selected row (even if the grid has been sorted) by using:

DataRowView row = dataTable.DefaultView[dataGrid.CurrentRowIndex];
0
alexatsearidgeAuthor Commented:
I hear what you're saying, I'm just not getting those results.  I don't think my view is sorting with the datagrid.  Here is my code:

// Bind the view to the dataset
myDataView = myDataSet.Tables[0].DefaultView;
myDataGrid.DataSource = myDataView;

// Get the value of the first cell in the current row
DataRowView row = dvCaseHistory[dataGrid.CurrentRowIndex];
scheduleID = Convert.ToInt32(row[0]);

See any reason why sorting the grid would not sort my view?  My view is in a separate class if that makes a difference.
0
dstanley9Commented:
Where are you getting dvCaseHistory?  Is it the same as myDataSet.Tables[0].DefaultView?  When I ran it the view index matched the selected row in the datagrid - I am using VS 2005, but I remember the behavior to be the same in VS 2003 as well.
0
alexatsearidgeAuthor Commented:
Yes sorry it is the same as myDataView.  Did you try sorting you datagrid and then seeing if the index returned the proper value?

Try putting a few rows ("A", "B", "C") at positions 1, 2 and 3 respectively.  
Then sort descending so that the order displayed is C, B, A.  Then select C and see if Row 1 in your dataview is returning C.
0
dstanley9Commented:
Yes.  I created a datagrid that alowed sorting (in my case I was sorting on the 5th column).  I then added a button that showed the 5th column in the row of the dataview at the selected index.

Here's my codebehind:


        private DataTable _data;
        public Form3()
        {
            InitializeComponent();

            _data = {code to get datatable}

            BindData();
        }

        private void BindData()
        {

            dataGrid1.DataSource = _data;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataRowView row = _data.DefaultView[dataGrid1.CurrentRowIndex];
            MessageBox.Show(_data.DefaultView.Sort);  // show the current sort order
            MessageBox.Show(row[4].ToString()); // show the value of the 5th column
        }

This showed me the correct sort order (based on the column header I clicked) and the value of the 5th column regardless of the sort order (I could sort on different columns, asc or desc, and it still worked).
I'm wondering if your dvCaseHistory is pointing to the same DataView as your DataSource.  
0
alexatsearidgeAuthor Commented:
Tried your code, I'm still getting the unsorted tables values.

Note:
Wehn I inserted your code with the messages boxes and ran it, they do not appear.  I traced through and for some reason it seems to jump past the code.  The code appears to run because one of my variables is populated.

I wonder if this has anything to do with the problem.
0
dstanley9Commented:
It probably didn't wire up the click event.  double-click on the button to create a click event, then paste the code in.  Are you certain that dvCaseHistory is pointing to the default view of the data table?  Can you post the code snippets that:

* assigns dvCaseHistory
* binds the datagrid
* gets the sleected index of the grid and tryies to get the matching row
0
alexatsearidgeAuthor Commented:
Got it, its working.  

Problem was that I was creating my view from a dataset, binding the view to the Grid, then when I was trying to get the cell value I was looking in the dataSet, not the view.  Using the view works perfect.
0
alexatsearidgeAuthor Commented:
Thanks so much for your patience.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.