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

RadGrid with RadioButton

5 Answers 410 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DEBAL
Top achievements
Rank 1
DEBAL asked on 16 Apr 2012, 03:57 PM
Hi Support
I'm doing work with telerik Radgrid in my asp.net application , the grid contain filtering , paging option, it looks nice . The data are comming from database to fill up the radgrid . now there is radiobutton with every row . I need to know suppose use click on one radio button and there is one button name delete or edit , the button is outside of radgrid , the row Id of that particular row . See the Image , in the attached file . Suppose user click on Container name row radio button (see the Image) and press the delete button I need to know The Row Id that is DisplayContinerId in my case.

In asp.net grid control it is done by below code but in Telerik pl guide me

protected void ButtonDelete_Click(object sender, EventArgs e)
{
 string id = string.Empty;
 //loop the GridView Rows
 for (int i = 0; i < GridView1.Rows.Count; i++){
      RadioButton rd= (RadioButton )GridView1.Rows[i].Cells[0].FindControl("radiobutton1"); //find the button
      if (rd.Checked)
      {
          id = GridView1.Rows[i].Cells[0].Text; // get the id of the field to be deleted
       }
 }
Thanks

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Apr 2012, 07:58 AM
Hello Debal,

One suggestion is you can set the DataKeyNames and access it as shown below.
aspx:
<MasterTableView   DataKeyNames="Id">
C#:
protected void ButtonDelete_Click(object sender, EventArgs e)
{
 foreach (GridDataItem item in grid1.Items)
 {
  RadioButton btn = (RadioButton)item.FindControl("radiobutton1");
  if (btn.Checked==true)
  {
    string value = item.GetDataKeyValue("Id").ToString();
      TableCell cell=(TableCell)item["Id"];
    int index = item.ItemIndex;
  }
 }
}

Thanks,
Princy.
0
DEBAL
Top achievements
Rank 1
answered on 18 Apr 2012, 06:05 AM
Hi Princy

thanks for your suggestion , I like telerik controls and support . The above code is work .
Now I have typical problem facing that :

My Radgrid containg Paging and pagesize = 10 property , I mean AllowPaging = true , and for that result the grid showing first 10 result in it . Suppose I have 100 records in database , so when I click 1,2,3,...etc in paging option in radgrid results are displaying in radgrid one by one. This is right.

My problem is if I write  AllowPaging = true , then the following code
foreach (GridDataItem item in grid1.Items)
 {
......
    }
Items are counting only 10 though my total records are 100 , so 100 row should count but I remove AllowPaging = true then all records are display in radgrid and radgrid becomes very long respective to the page .
How can handle this ???
0
Shinu
Top achievements
Rank 2
answered on 18 Apr 2012, 06:33 AM
Hello Debal,

When paging is enabled in the grid, a solution would be to temporarily disable paging, loop through all the items in the grid, and then enable paging back.
 C#:
grid1.AllowPaging = false;
//your code
grid1.AllowPaging = true;
grid1.Rebind();

Thanks,
Shinu.
0
DEBAL
Top achievements
Rank 1
answered on 19 Apr 2012, 03:44 PM
Hi Shinu
I tried a lot with this but can't able to show my output .
protected void ButtonDelete_Click(object sender, EventArgs e)
{
 int Questionnumber = 100// this number is actually pass through method parameter .

 RadGrid1.Allowpaging = false;
 RadGrid1.ReBind();
 foreach (GridDataItem item in grid1.Items)
 {
  RadioButton btn = (RadioButton)item.FindControl("radiobutton1");
  string value = item.GetDataKeyValue("Questionnumber").ToString(); 
    if( value == Questionnumber)
          {
                 btn.checked = true; // see this is mandatory , because Question number is coming from a method which is related to a
// treeview selected node , and that node corresponding row should be selected in RadGrid , here in this button click have nothing to do with
//treeview , so we hard coded the QuestionNumber 100
          }
 
}
RadGrid1.Allowpaging = true ;
 RadGrid1.PageSize = 20; // If I make this Size my selected Radio button is Not Showing in DataGrid
//and if I make it 100 or above then it is Showing .

RadGrid.ReBind(); //If I make it ReBind then it is my radio button is not showing  selected row in RadGrid .
 
}

This is very important , other wise my telerik control becomes unused. See in aspx page I don't set any
AllowPaging = true .
Pl give me any suggestion pl

One Solution may be , though the grid  may have thousands of records getting from database , so see the paging
where the selected row is existing , suppose radiobutton.selected row is in page 5 so start paging and load
grid data from 5th page and there is option previous and next paging...something like that , how can I do
0
Shinu
Top achievements
Rank 2
answered on 23 Apr 2012, 12:43 PM
Hello Debal,

You can save the checked RadionButton’s row keyValue in a session ArrayList. Then In Prerender event take it from session and persist the checked state. And you can use the CurrentPageIndex property to change the pageIndex.

Here I am pasting the full code that I tried which worked as expected.
aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="true" PageSize="4" OnPreRender="RadGrid1_PreRender">
  <MasterTableView DataKeyNames="EmployeeID">
     <Columns>
      <telerik:GridBoundColumn DataField="EmployeeID" UniqueName="EmployeeID" HeaderText="EmployeeID"></telerik:GridBoundColumn>
      <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName"></telerik:GridBoundColumn>
      <telerik:GridTemplateColumn>
         <ItemTemplate>
           <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" />
         </ItemTemplate>
        </telerik:GridTemplateColumn>
        </Columns>
  </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
C#:
public static ArrayList selectedItems;
    protected void Button1_Click(object sender, EventArgs e)
    {
     selectedItems = new ArrayList();
     int rowIndex=0;
 
        RadGrid1.AllowPaging = false;
        RadGrid1.Rebind();
        foreach (GridDataItem item in RadGrid1.Items)
        {
            RadioButton btn = (RadioButton)item.FindControl("radiobutton1");
            string value = item.GetDataKeyValue("EmployeeID").ToString();
            if (value == "8")//your condition
            {
                rowIndex = item.ItemIndex;
                selectedItems.Add(value);
                Session["selectedItems"] = selectedItems;
            }
        }
        RadGrid1.AllowPaging = true;
        RadGrid1.CurrentPageIndex = rowIndex / RadGrid1.PageSize;
        RadGrid1.Rebind();
    }
 
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (Session["selectedItems"] != null)
        {
            ArrayList selectedItems = (ArrayList)Session["selectedItems"];
            Int16 stackIndex;
            for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
            {
                string curItem = selectedItems[stackIndex].ToString();
                foreach (GridItem item in RadGrid1.MasterTableView.Items)
                {
                    if (item is GridDataItem)
                    {
                        GridDataItem dataItem = (GridDataItem)item;
                        if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["EmployeeID"].ToString()))
                        {
                            RadioButton btn = (RadioButton)item.FindControl("radiobutton1");
                            btn.Checked = true;
                            
                            break;
                        }
                    }
                }
            }
            
        }
    }

Thanks,
Shinu.
Tags
Grid
Asked by
DEBAL
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
DEBAL
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or