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

How to create customize rows in RadGrid?

1 Answer 147 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anh Vu
Top achievements
Rank 1
Anh Vu asked on 02 Feb 2013, 03:51 PM
Hello,
I am brand new to RadGrid.
I working on a grid that have only 3 columns.

Firstly, data are retrieved from database into a DataTable, like this:
 static DataTable GetData()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Name"typeof(string));
        table.Columns.Add("Activate"typeof(string));
        table.Columns.Add("Actions"typeof(string));        
 
        table.Rows.Add("Toilets", "Activate""Duplicate Delete");
        table.Rows.Add("Swimming pools", "Activate", "Duplicate Delete");
        table.Rows.Add("Pumps", "Deactivate", "Duplicate Delete");
        table.Rows.Add("Taps", "", "Duplicate Delete");
        table.Rows.Add("Boiler""Activate""Duplicate Delete");
        return table;
    }"

then bind to radgrid using: .DataScource = dataTable;

I want to archive the followings:
1. Active column: i wanna use Image button to render "Activate", "Deactivate" command. When user click those buttons, how can i know from which rows, the buttons were clicked. If the data cell is empty, then do not show any button.
2. I wanna replace Duplicate and Delete with two image buttons inside the same cell.

It seems that i should use ItemTemplate or something like that but after reading documents from telerik, i still can not find out the solution. I am really appreciate your help. Thanks in advanced.

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Feb 2013, 05:54 PM
Hello,
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
           OnItemDataBound="RadGrid1_ItemDataBound" AllowFilteringByColumn="true" AllowMultiRowEdit="true"
           OnItemCommand="RadGrid1_ItemCommand">
           <MasterTableView EditMode="InPlace" CommandItemDisplay="Top">
               <Columns>
                   <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                   </telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <asp:ImageButton ID="ImageButton1" runat="server" />
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "SetActivate")
            {
                GridDataItem item = e.Item as GridDataItem;
                string strnaem = item["Name"].Text.ToString(); // access name column text
                //perform Table Update opration
                RadGrid1.Rebind();
            }
            else if (e.CommandName == "SetDeactivate")
            {
                GridDataItem item = e.Item as GridDataItem;
                string strnaem = item["Name"].Text.ToString(); // access name column text
                //perform Table Update opration
                RadGrid1.Rebind();
            }
        }
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if(e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                ImageButton ImageButton1 = item.FindControl("ImageButton1") as ImageButton;
                DataRowView dr = e.Item.DataItem as DataRowView;
                if (dr["Activate"] == "Activate")
                {
                    ImageButton1.AlternateText = "Activate";
                    ImageButton1.ImageUrl = "Set Your Image URL";
                    ImageButton1.CommandName = "SetDeactivate";
                }
                else
                {
                    ImageButton1.AlternateText = "Deactivate";
                    ImageButton1.ImageUrl = "Set Your Image URL";
                    ImageButton1.CommandName = "SetActivate";
                }
            }
}
 
 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
    
 
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Activate", typeof(string));
            table.Columns.Add("Actions", typeof(string));
 
            table.Rows.Add("Toilets", "Activate", "Duplicate Delete");
            table.Rows.Add("Swimming pools", "Activate", "Duplicate Delete");
            table.Rows.Add("Pumps", "Deactivate", "Duplicate Delete");
            table.Rows.Add("Taps", "", "Duplicate Delete");
            table.Rows.Add("Boiler", "Activate", "Duplicate Delete");
            RadGrid1.DataSource = table;
        }

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