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

RadGrid GridButtonColumn

1 Answer 905 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 1
Brett asked on 02 Oct 2012, 03:02 PM
How do I work with the RadGrid GridButtonColumn if in case I want to use the button to edit a record?  I have not seen any documentation as far as this control is concerned, can somebody point me in the right direction as far as programming a datasource with this control? 
Thank you!

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Oct 2012, 05:01 PM
Hello,

//InBuilt EditCommandColumn.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
             
            onupdatecommand="RadGrid1_UpdateCommand">
            <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
              new { ID = 1, Name ="aaa"},
              new { ID = 2, Name = "bbb"},
              new { ID = 3, Name = "ccc"},
              new { ID = 4, Name = "ddd"},
               new { ID = 5, Name ="eee"},
               new { ID = 6, Name ="aaa"},
              new { ID = 7, Name = "bbb"},
              new { ID = 8, Name = "ccc"},
              new { ID = 9, Name = "ddd"},
               new { ID = 10, Name ="eee"}
            };
            RadGrid1.DataSource = data;
        }
 
 
 
 protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            // Perform your Update Logic here
 
        }

// Using TemplateColumn
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand">
            <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn>
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
                            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
 
C# code is same as above.

// ButtonColumn
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
           OnItemDataBound="RadGrid1_ItemDataBound"  OnUpdateCommand="RadGrid1_UpdateCommand">
           <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
               <Columns>
                   <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                   </telerik:GridBoundColumn>
                  <telerik:GridButtonColumn CommandName="Edit" Text="Edit" UniqueName="EditCommand"></telerik:GridButtonColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            // Perform your Update Logic here
 
        }
 
 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
              new { ID = 1, Name ="aaa"},
              new { ID = 2, Name = "bbb"},
              new { ID = 3, Name = "ccc"},
              new { ID = 4, Name = "ddd"},
               new { ID = 5, Name ="eee"},
               new { ID = 6, Name ="aaa"},
              new { ID = 7, Name = "bbb"},
              new { ID = 8, Name = "ccc"},
              new { ID = 9, Name = "ddd"},
               new { ID = 10, Name ="eee"}
            };
            RadGrid1.DataSource = data;
        }
 
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                LinkButton btnEdit = item["EditCommand"].Controls[0] as LinkButton;
                btnEdit.CommandName = "Edit";
                btnEdit.Text = "Edit";
            }
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = e.Item as GridEditableItem;
                LinkButton btnEdit = item["EditCommand"].Controls[0] as LinkButton;
                btnEdit.CommandName = "Update";
                btnEdit.Text = "Update";
            }
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Brett
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or