Access Rows and Cells in a Grid

4 posts, 0 answers
  1. Chris
    Chris avatar
    120 posts
    Member since:
    Jun 2006

    Posted 24 Sep 2009 Link to this post

    I am trying to loop through a grid and replace some values in certain cells. I am basically taking values from a text box, matching it to values in the grid and replacing those values with the value in another text box.  I can do this without any issue using a standard GridView.  How do I do this with RadGrid. RadGrid seems to not have a Rows object.

    Here is my code that works fine with a GridView:

    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Telerik.Web.UI;

    public partial class Default3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            GenerateUniqueData(3);
        }
        private void GenerateUniqueData(int name)
        {
            string initialnamevalue = this.TextBox1.Text;


            for (int i = 1; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[name].Text == initialnamevalue)
                    GridView1.Rows[i].Cells[name].Text = this.TextBox2.Text;
                else
                    initialnamevalue = GridView1.Rows[i].Cells[name].Text;
            }
        }
    }

  2. Pavlina
    Admin
    Pavlina avatar
    5388 posts
    Member since:
    Dec 2014

    Posted 24 Sep 2009 Link to this post

    Hi Chris,

    How to access rows and cells in a RadGrid control you can learn from the following help article:
    Accessing cells and rows

    I hope this information helps. Please do not hesitate to contact us if other question arise.

    All the best,
    Pavlina
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

  3. Chris
    Chris avatar
    120 posts
    Member since:
    Jun 2006

    Posted 24 Sep 2009 Link to this post

    Would you mind providing a simple example based on my code above?  It should only be a few line of code for you guys.  In my example, I am looping through the GridView, getting the value from the 3rd column and checking to see if the values in the cells in the 3rd column are the same as my textbox value.  With RadGrid, I believe I can check for UniqueName instead of defining the column number. Thanks for you help.

  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Sep 2009 Link to this post

    Hello Chris,

    You can try out the following code  to achieve the required, using RadGrid:
    aspx:

     <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" UniqueName="Column">                       
     </telerik:GridBoundColumn>  

    c#:
     private void GenerateUniqueData() 
        { 
            string initialnamevalue = this.TextBox1.Text; 
     
            foreach(GridDataItem item in RadGrid1.MasterTableView.Items) 
            { 
                if(item["TransactionsID"].Text == initialnamevalue) 
                     item["TransactionsID"].Text = this.TextBox2.Text; 
               
            } 
        } 

    Hope this help you gets started..
    Princy.

Back to Top