This is a migrated thread and some comments may be shown as answers.

Adding Control(checkBox) in RadGrid

8 Answers 435 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dip
Top achievements
Rank 1
Dip asked on 08 Nov 2008, 04:43 AM
I Have a RadGrid with some field null and some field boolean Value...I have to add control in cetain Cell of RaDGrid....
Protected void RadGrid1_ItemDataBound(object sender,GridItemEventArgs e)
{
     if(e.item.is GridDataItem)
         {
                    DataRowView Rv=(DataRowView)e.Item.DataItem;
                    (Now ...)

          }
}

Till Now I can Access Row of the Grid..Now I have to Access Cell so that I can add control
eg: cell.control.add(CheckBox)..some like this...

Thanks,
DIP

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Nov 2008, 05:23 AM
Hello,

You can try out the following code to add a control to a cell based on a condition. Cells can be accessed by using the column UniqueName property for columns.

cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            CheckBox check = new CheckBox(); 
            check.ID = "CheckBox1"
            //if condition is met then            
            dataItem["ColumnUniqueName"].Controls.Add(check); 
             
        } 
     } 

Thanks
Princy.
0
Dip
Top achievements
Rank 1
answered on 10 Nov 2008, 09:54 PM
that is COOL...

But is there a better way of doing it.....What I was looking for is to come up with column indexes rather than column name ...
If there a a way Please have a sample code on this post...


thanks,
DIP
0
Shinu
Top achievements
Rank 2
answered on 11 Nov 2008, 06:21 AM
Hi Dip,

Try adding the CheckBox to the desired cell as shown below.

CS:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
             
            CheckBox check = new CheckBox(); 
            check.ID = "CheckBox1"
            // check for the condition
            if (e.Item.Cells[2].Text == "MyText") 
                e.Item.Cells[2].Controls.Add(check); 
 
        }  
    } 

Using indexes to access individual cells in the Cells collection of a row is not a reliable method of obtaining a cell in a particular column.
The index of individual columns can change on the client on  column reordering and grouping.The reliable way of locating the cell in a particular column
is using the UniqueName property of a column.

Thanks
Shinu.
0
Dip
Top achievements
Rank 1
answered on 11 Nov 2008, 11:54 PM
Thanks ..

Well, that still doesn't solve my problem...Let me brief it to you and may be you can give me a better solution...I am freaking out..
I have a DataTable that is in this structure..
Resource          View        Modify      
------------------------------------------
Network             true          false
Revenue                           false
Status               false        

Network             true          false


which I bind to RadGrid ..Place where states "true" I need a checkBox with checked .."False" needs CheckBox with unchecked and null is Label...I am checking this on DataItemBound using the idea stated by you..But the only reason i wanted it to do with index was because I have about 16 columns.....How can I iterate and just do it once?

Now the user can check and uncheck the checkBox...Which is also ok.... now the problem comes when use Click a button and I have to update all the checkboxes...
1) I have to Access each cell in the RadGrid + header("View/Modify") +Col1(Network/Revenue/Status) to update in datatable
2) I don't seem to find checkbox in the cells...
-----------------------Used-----------------------
RadGrid Grid=this.FindControl("RadGrid1") as RadGrid;
Grid.Cells[1].Findcontrol("Chk") as CheckBox;  (I don't seems to find CheckBox)..


Thanks,
A ton....DIP 
0
Shinu
Top achievements
Rank 2
answered on 12 Nov 2008, 06:21 AM
Hi Dip,

One suggestion will be use GridTemplateColumn with a CheckBox in its ItemTemplate and bind its DataField to 'View' /  'Modify' . You can set the View / modify field as the DataKeyName and depending on its value you can set the  Checked/Visible property of the CheckBox in the ItemDataBound event.

ASPX:
 <telerik:GridTemplateColumn UniqueName="View"  HeaderText="View"  DataField="View" > 
      <ItemTemplate> 
          <asp:CheckBox ID="CheckBox1"    runat="server" /> 
      </ItemTemplate> 
    </telerik:GridTemplateColumn> 

//To set the DataKeyName
<MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="View"    > 
  


CS:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            CheckBox chkbx = (CheckBox)item["View"].FindControl("CheckBox1"); 
            string key = item.GetDataKeyValue("View").ToString(); 
            if (key == "True") 
                chkbx.Checked = true
            else if (key == "False") 
                chkbx.Checked = false
            else 
            { 
                //to hide the CheckBox when the value is null 
                chkbx.Visible = false
                //to add a label  
                Label lbl = new Label(); 
                lbl.ID = "Label2"
                lbl.Text = "Empty"
                item["View"].Controls.Add(lbl); 
            } 
        } 
        
    } 


//to access the CheckBox in the Button click event.
protected void Button1_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            CheckBox chkbx = (CheckBox)item["View"].FindControl("CheckBox1"); 
        } 
        
    } 


Thanks
Shinu
0
Dip
Top achievements
Rank 1
answered on 13 Nov 2008, 01:15 AM
Thanks Shinu,
I can't use that because DataKeyValue is related to one column and to access other value I have to connect to database again...
Anyway I figured out a way which is primitive, but works like a charm....

Got the numver of item and got the number of cells..
Item represents -- Row
cells represent ---Column
then I iterate with 2 for loops..
RadGrid1.MasterTableView.Items[Row_Index].Cells[Column_Index].Text....

But Thanks for all the replys

Thanks,
DIP
0
Accepted
Shinu
Top achievements
Rank 2
answered on 13 Nov 2008, 06:18 AM
Hi Dip,

I would like to let you know that you can set more than one field as the  key value in the DataKeyNames array.

ASPX:
<MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="View, Modify"    >  

Regards
Shinu.
0
Dip
Top achievements
Rank 1
answered on 13 Nov 2008, 08:16 PM
You see ..You should have given this hint long time ago....I implemented exactly the way you told me and is pity good in Shape...

thanks,
DIP
Tags
Grid
Asked by
Dip
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dip
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or