Cannot Access Data in RadGrid in VS2010 Visual Web Part Project

4 posts, 0 answers
  1. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 13 Oct 2010 Link to this post

    I'm creating my first Visual Web Part project with Visual Studio 2010 (C#). The purpose of this simple project is to become better aquainted with the RadGrid control. There are basically two parts to it:

    1. Add RadGrid control and bind to datasource using C#. The RadGrid has an edit and delete column.

    2. Add a RadTextBox control to display the captured content of a specific cell when I click on the update option when editing the record.

    Part 1 is pretty straightforward and seems to work fine. Here is the code that I run upon page load:

    private void bindGrid(RadGrid grid)
        {
            string qry = "SELECT Recipient, Email FROM Table1";
            SqlConnection conn = new SqlConnection(connection string);
            SqlCommand cmd = new SqlCommand(qry, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable recipients = new DataTable();
                try
                {
                    conn.Open();
                    adapter.Fill(recipients);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                   grid.DataSource = recipients;
                   grid.DataBind();
                   conn.Close();
                }
        }

    The problem is with part 2. When I click on edit then update I want to capture the value in that row for the column labeled "Recipient".
    I try to do this with the following code:

    protected void Update_Command(object sender, GridCommandEventArgs e)
      {
           
          if (e.Item is GridDataItem)
          {
              GridDataItem dataItem = e.Item as GridDataItem;
              RadTextBox1.Text = dataItem["recipient"].Text.ToString(); 
          }
          else if (e.Item is GridEditFormItem)
          {
          }
      }

    Instead of getting the persons name displayed in the cell, all I get is "&nbsp"; I've tried this several different ways, even using
    the NeedDataSource method. The result is always the same.

    Out of curiousity I counted the columns in the RadGrid immediately after binding with RadGrid1.columns.count and get 0. So the RadGrid displays the data from the datasource properly on page load, but I can't seem to access any of that data.

    I can't figure this out and any guidance would be greatly appreciated.


  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 18 Oct 2010 Link to this post

    Hi, John,

    The best way to get hand on the newly-enetred values in the edit form upon hitting the update button is as follows:

    Hashtable newValues = new Hashtable();
    ((GridEditableItem)e.Item).ExtractValues(newValues);
    RadTextBox1.Text = newVAlues["recepient"].ToString();

    Hope it helps.


    Tsvetoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

  3. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 19 Oct 2010 Link to this post

    Hello Tsvetoslav,

    Thank you for your response. The code works to capture the current value in the recipient field, but not the new one.

    For example: 
            a. The current recipient is John Smith
            b. I click Edit
            c. Change the name to Giuseppe Garibaldi
            d. Click Update
            e. The name captured and displayed in the text box is John Smith

    I need the new value so I can update the database record on my server. (I'm glad to be making progress though.)

    Another question I have is regarding the documentation. The instructions under Radgrid > Grid rows > Accessing cells and rows in the product documentation seems very straightforward and there is no mention of hash tables. I'm just curious as to why the instructions wouldn't work in my situation.

    Thanks for you help.

    John
  4. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 25 Oct 2010 Link to this post

    Hi John,

    I suspect that the issue is due to incorrect binding of the grid control. Could you paste you complete aspx mark-up and code behind.

    As for the documentation, the help topic you have mentioned is not directly related to the application requirement you have. That one is the topic of a different help article:
    http://www.telerik.com/help/aspnet-ajax/grdupdatinginplaceandeditforms.html

    Hope this information will prove helpful.

    Regards,
    Tsvetoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top