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

Problems with RadGrid Rebind after ItemCommand

1 Answer 423 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Malichone
Top achievements
Rank 1
Malichone asked on 04 Jan 2012, 04:09 PM
Currently I have a grid with multiple rows. The last column of the grid is an imagebutton inside a GridTemplateColumn to "remove" the row. What i mean by remove is actually change the value of another column in a GridBoundColumn and hide the desired row. 

I currently have an itemcommand and commandname on the imagebutton.
<asp:ImageButton runat="server" ID="imgRemoveCostCenter" ImageUrl="~/Images/Remove.png" OnItemCommand="rad_CostCenters_ItemCommand" CommandName="HideCostCenter" />

The grid itself has the OnItemCommand event bound.
<telerik:RadGrid ID="rad_CostCenters" OnItemCommand="rad_CostCenters_ItemCommand" AllowMultiRowEdit="true" AutoGenerateColumns="false" EnableTheming="true" Skin="Default" AllowSorting="false" runat="server" ShowHeader="true" OnItemDataBound="Rad_ManagersList_ItemDataBound">

Have a GridTemplateColumn to hold the "ActionID"
<telerik:GridBoundColumn UniqueName="ActionID" DataField="ActionID" Visible="false" />

and finally have the function in the code-behind:
protected void rad_CostCenters_ItemCommand(object sender, GridCommandEventArgs e)
{
    GridDataItem item = (GridDataItem)e.Item;
 
    int _ActionID = Convert.ToInt16(item["ActionID"].Text);
 
    if (e.CommandName == "HideCostCenter")
    {
        _ActionID = -1;
    }
    rad_CostCenters.Rebind();
}
protected void Rad_ManagersList_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DataRowView drv = (DataRowView)e.Item.DataItem;
 
        int _ActionID = Convert.ToInt16(item["ActionID"].Text);
 
        if (_ActionID == -1)
        { item.Display = false; }
    }
}

How can I get this to set the actionID so it can be rebound?

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Jan 2012, 08:28 PM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
          AllowSorting="true" ShowGroupPanel="true" GroupingEnabled="true" OnItemDataBound="RadGrid1_ItemDataBound"
          OnInsertCommand="RadGrid1_InsertCommand"
          onitemcommand="RadGrid1_ItemCommand">
          <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
              <Columns>
                  <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                  </telerik:GridBoundColumn>
                  <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                  </telerik:GridBoundColumn>
                  
                  <telerik:GridButtonColumn CommandName="Remove" Text="Remove">
                  </telerik:GridButtonColumn>
                   
                  <telerik:GridEditCommandColumn>
                  </telerik:GridEditCommandColumn>
              </Columns>
          </MasterTableView>
          <ClientSettings AllowDragToGroup="true">
          </ClientSettings>
      </telerik:RadGrid>
public string _RemoveID
       {
           get
           {
               if (ViewState["Text"] == null)
               {
                   return ",";
               }
               else
               {
                   return ViewState["Text"].ToString();
               }
           }
 
           set
           {
               ViewState["Text"] = value;
           }
       }
 
        
 
       protected void Page_Load(object sender, EventArgs e)
       {
          
 
           if (!IsPostBack)
           {
               _RemoveID = null;
           }
 
       }
 
       protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("ID", typeof(System.Int32));
           dt.Columns.Add("Name", typeof(System.String));
           dt.Columns.Add("SrNo", typeof(System.Int32));
           dt.Rows.Add("1", "Name1", "1");
           dt.Rows.Add("2", "Name2", "2");
           dt.Rows.Add("3", "Name3", "3");
           dt.Rows.Add("4", "Name4", "4");
 
           RadGrid1.DataSource = dt;
       }
 
       protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
 
           if (e.Item is GridDataItem)
           {
               GridDataItem item = e.Item as GridDataItem;
               if (_RemoveID.IndexOf("," + item.GetDataKeyValue("ID").ToString() + ",") > -1)
               {
                   item.Display = false;
               }
 
           }
            
       }
 
       protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
       {
            
       }
 
       protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
       {
           if (e.CommandName == "Remove")
           {
               GridDataItem item = e.Item as GridDataItem;
               _RemoveID += item.GetDataKeyValue("ID").ToString() + ",";
               RadGrid1.Rebind();
           }
       }


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