Solved

Does a DataGrid have a value property?

Posted on 2007-03-20
15
212 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:alexatsearidge
  • 8
  • 7
15 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 18756839
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
 

Author Comment

by:alexatsearidge
ID: 18756904
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 18756958
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:alexatsearidge
ID: 18757106
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
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 18757259
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
 

Author Comment

by:alexatsearidge
ID: 18757298
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 18757454
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
 

Author Comment

by:alexatsearidge
ID: 18757811
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 18757844
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
 

Author Comment

by:alexatsearidge
ID: 18757905
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 18757961
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
 

Author Comment

by:alexatsearidge
ID: 18758056
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
 
LVL 25

Expert Comment

by:dstanley9
ID: 18758089
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
 

Author Comment

by:alexatsearidge
ID: 18758144
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
 

Author Comment

by:alexatsearidge
ID: 18758145
Thanks so much for your patience.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question