Skip to content

Infragistics Community Forum / Web / Ultimate UI for ASP.NET Web Forms / Retrieve Data from WebDataGrid.Rows

Retrieve Data from WebDataGrid.Rows

New Discussion
Denis
Denis asked on Jun 15, 2011 8:12 PM

I need to retrieve the data contained inside a WebDataGrid.  What is the best way to do it?

At first I thought I could do something like this below (ADO.NET DataTable style) :

foreach (Infragistics.Web.UI.GridControls.GridRecord rec in WebDataGrid1.Rows)
{
     rec[
"MyColumn"];  // Do something with the value here
}

I know I can get the value using the column index rec.Items[0].Value .  Is there a way I can use the column Key name?

Thanks

Daniel

Sign In to post a reply

Replies

  • 0
    [Infragistics] Nikola Genov
    [Infragistics] Nikola Genov answered on Feb 9, 2009 2:29 PM

    Hi Daniel,

    I recommend you to use the DataItem property, for example if you bind to a DataSet/DataTable/SqlDataSource you just need to cast the data item as a DataRowView:

      foreach (Infragistics.Web.UI.GridControls.GridRecord rec in WebDataGrid1.Rows)
      {
          object value = ((DataRowView)rec.DataItem)["FirstName"];
      }

    Hope that helps!

    • 0
      Denis
      Denis answered on Feb 9, 2009 2:39 PM

      That would be great!… but my rec.DataItem is always null… 🙁

      I am loading the Grid from a button event and I load it using a DataTable in memory.

      Daniel

      • 0
        [Infragistics] Nikola Genov
        [Infragistics] Nikola Genov answered on Feb 9, 2009 5:42 PM

        [quote user="devdti"]
        That would be great!… but my rec.DataItem is always null
        [/quote]

        I'm not sure what's your exact setup, is DataItem happening to be null on postback? When restoring data from viewstate (on postback) data items may be null.

        The second option I can suggest is this:

        int columnIndex = WebDataGrid1.Columns["LastName"].Index;
        foreach (Infragistics.Web.UI.GridControls.GridRecord rec in WebDataGrid1.Rows)
        {
            object value = rec.Items.GetValue(columnIndex);
        }

         

      • 0
        Denis
        Denis answered on Feb 13, 2009 1:42 PM

        After verification, the DataItem contains the data just after I set the Grid.DataSource property.

        But after a postback the DataItem is null.  I have set the Grid.ViewState = true and Grid.DataViewState = true.  I thought that it would keep those values in the ViewState.

        That would be REALLY annoying to have to retrieve the index of each columns to retrieve the data.  I have about 20 columns I need to retrieve values.

        Isn't there any other way to do that? 🙁

        Thanks 

        Daniel

         

         

      • 0
        [Infragistics] Nikola Genov
        [Infragistics] Nikola Genov answered on Feb 13, 2009 2:04 PM

        WebDataGrid does not serialize the whole DataItem in the view state, only the grid record values. That's why DataItem is null on postback. If you still want to use it you may disable data view state and set grid DataSource on postback.

        [quote user="devdti"]That would be REALLY annoying to have to retrieve the index of each columns to retrieve the data. [/quote]

        I see, surely this is annoying. I would suggest you to add a simple method which returns record value by column name:

        private object GetRecordValue(GridRecord rec, string columnName)
        {
            int columnIndex = WebDataGrid1.Columns[columnName].Index;
            return rec.Items.GetValue(columnIndex);

        foreach (Infragistics.Web.UI.GridControls.GridRecord rec in WebDataGrid1.Rows)
        {
            object value = GetRecordValue(rec, "LastName");
        }

        Hope that's ok, even better if you are working with .Net framework 3.5 you can add this as extension method to GridRecord.

         

      • 0
        Patrick
        Patrick answered on Feb 16, 2009 2:07 AM

        Even with EnableDataViewState set to true, if the user edits any data, there is no way to properly retrieve the information during postback.  How am I supposed to retrieve the edited fields?  Seems like this would be a common use for the Webdatagrids.

        I should add I preload the data using a Datatable, so I can see the previous values, just not the new edited ones.  Sometimes when I read the data during postback using the above code, I got all sorts of jumbled data, meaning different column data on different rows.  Is there a way to manipulate the viewstate in javascript to achieve what I want or come up with some other solution to simply read the changed rows data properly during postback?

      • 0
        Alex Kartavov
        Alex Kartavov answered on Feb 17, 2009 2:51 PM

        Off the EditingCore behavior there is the RowUpdating event, which is

        fired before the row is actually updated. Off of its event args you can

        get the old and new values hashtables. The keys are the field names.

        You don't have to put your data in the view state to get those values in

        that event, they are brought from the clients.

        Just to clarify the EnableDataViewState flag does in fact put all of the

        data into the ViewState. It instructs the grid to reflect off of the

        data source records and all simple types are stored for the next

        session. The DataItems off the rows are available after the post back if

        the flag is set to True.

      • 0
        Alex Kartavov
        Alex Kartavov answered on Aug 11, 2009 10:31 PM

        My bad. The DataItems are still null in the case of EnableDataViewState. However the previous values should be available of off the row that is being updated. If you don't see that being the case, I am curious to investigate that more. Thanks.

    • 0
      Adrian
      Adrian answered on Jul 27, 2010 10:17 AM

      Hi all

      These seems only to work when using DataSource controls?

      In our project I assigne directly a IList<UserDefinedDTO> to the DataSource of the WebDataGrid as in the following lines:

      IList<MyUserDefinedDTO> myData = GetTheData();
      MyWebDataGrid.DataSource = myData;

      But when trying to cast back the row.DataItem

      MyUserDefinedDTO data = (MyUserDefinedDTO)row.DataItem
      an InvalidCastException is thrown (sorry for the german error message):

      {"Das Objekt des Typs Infragistics.Web.UI.Framework.Data.DataRecord kann nicht in Typ MyUserDefinedDTO umgewandelt werden."}

      Is it at all possible to cast back the row.DataItem in situations as described above?

      Regards
      Adrian

  • 0
    Nick
    Nick answered on Jun 15, 2011 8:12 PM

    Since I was only concerned about reading the data, my solution was to
    write a function to convert the DataGridRow to a HashTable. The
    function returns a hashtable with the keys set to the columns' Key
    values.  If you use the same string in your column DataFieldName and Key
    properties, you will get a hashtable very similar to as
    the HashTable returned by RowUpdatingEventArgs.Values

     

        Private Function GridRecordToHashTable(ByVal gridRecord As GridRecord) As Hashtable

            Dim hashTable As New Hashtable()

            Dim i As Integer = 0
            ' for each didn't work here, it would exit loop after first iteration
            For i = 0 To gridRecord.Items.Count – 1
                Dim key As String = gridRecord.Items(i).Column.Key
                Dim value As String = gridRecord.Items(i).Text
                hashTable(key) = value
            Next

            Return hashTable

        End Function

     

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Denis
Favorites
0
Replies
10
Created On
Jun 15, 2011
Last Post
15 years, 3 months ago

Suggested Discussions

Created by

Created on

Jun 15, 2011 8:12 PM

Last activity on

Jun 15, 2011 8:12 PM