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

Item Template in RadGrid

2 Answers 458 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JD
Top achievements
Rank 1
JD asked on 29 Sep 2011, 06:34 PM
Hello,

I have a Radgrid  in which I want to add a button as an item template ..I have to add it programatically as I have to check for certain conditions..so it won't go for all the grid rows...and then when this button is clikced I want to get data about the row in which this button was pressed.Can someone please tell me how I can achieve this...Let me know if i need to explain more.

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Sep 2011, 07:17 PM
Hello,

Please check below code snippet.
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
            OnNeedDataSource="RadGrid1_NeedDataSource"
            onitemdatabound="RadGrid1_ItemDataBound">
            <MasterTableView
             DataKeyNames="ID">
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:Button ID="btnTest" runat="server" Text="Button" OnClick="btnTest_Click" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                new { ID = 1, Name ="Name1"},
                new { ID = 2, Name = "Name2"},
                new { ID = 3, Name = "Name3"},
                new { ID = 4, Name = "Name4"},
                new { ID = 5, Name = "Name5"}
            };
 
            RadGrid1.DataSource = data;
        }
 
        protected void btnTest_Click(object sender, EventArgs e)
        {
            Button btnTest = sender as Button;
            GridDataItem item = btnTest.NamingContainer as GridDataItem;
            Response.Write(item.GetDataKeyValue("ID").ToString());
        }
 
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                Button btnTest = item.FindControl("btnTest") as Button;
 
                string ID = item.GetDataKeyValue("ID").ToString(); // get value from datakey
                string Name = item["Name"].Text; // get value from column
                if (ID == "1")
                {
                    btnTest.Style.Add("Display", "none");
                }
            }
        }

Let me know if any concern.

Thanks,
Jayesh Goyani
0
JD
Top achievements
Rank 1
answered on 29 Sep 2011, 08:10 PM
Perfect ...
Tags
Grid
Asked by
JD
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
JD
Top achievements
Rank 1
Share this question
or