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

Problem with dynamically added button column

6 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Veena
Top achievements
Rank 1
Veena asked on 11 Jun 2013, 09:24 AM
I am adding the columns to my radgrid dynamically, in page_load as shown in the link,
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section1
 using the method
- declare the RadGrid control in the ASPX file and define its structure in the code-behind.

Now, the issue is that, whenever a postback occurs in the page, my deletecolumn disappears. Obviously this is because I am checking for ispostback in my CreateGridDynamically() method. But then, why all other columns are populated after postback then?

What I need to have is a deletecolumn(either template column or gridbuttoncolumn). I tried to add the template column. but as it was written in that link, it needed to call the method in page_init. Then my needdatasource didn’t work. In effect, my grid didn’t bind with data at all. So, I was trying to add the gridbutton column. now, that works (I mean delete column is shown initially). But whenever a postback occurs in the page(may be with some other ddl change), the column becomes hidden. I removed the ispostback checking , but that resulted in all columns added twice to the grid, after a postback. So, is there any other grid event where I need to add the deletecolumn to make it show after postback, or what may be the wrong thing in my code?

btw, i can click on my delete button and it fires the itemcommand. but even if i added the commandname for the deletecolumn, the e.commandName appears as "" in the itemcommand. Does it have a relation to what am doing? what can i do to get the actual command name instead?

Please help me to get aroud this. Any help is appreciated.

Veena

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 11 Jun 2013, 09:56 AM
Hi Veena,

May i suggest,since you are adding the RadGrid in the ASPX file,then set the property of RadGrid, AutoGenerateDeleteColumn= true .
Please try if this helps.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteColumn="true">       
</telerik:RadGrid>

Thanks
Princy
0
Veena
Top achievements
Rank 1
answered on 11 Jun 2013, 10:27 AM

Hi Princy,

    Thank you for the quick reply.

    This completely fixes the issue for the Delete column, but in a scenario where i want to add my own GridButtonColumn, how do I make it persist postbacks and pass a valid CommandName?

Hope you can help.

Veena
0
Princy
Top achievements
Rank 1
answered on 12 Jun 2013, 04:13 AM
Hi,

Im not sure if this will help,but, if u want to dynamically create columns do it in Page_Load event, make sure IsPostBack is False, and incase you want to add template columns,do it in Page_init event.
This documentation clearly mentions how to add dynamically and all that you need.I guess you have already checked it.
Let me know if you have any queries and elaborate on your requirements.

Thanks,
Princy
0
Veena
Top achievements
Rank 1
answered on 12 Jun 2013, 09:21 AM
Hi,

    just telling you my exact requirement, I have a custom gridbutton column with a custom commadname and the commandname is not passing at all on click of the button column. I need to do a specific task with that column. Gridbound columnworks great, and now delete column works great after setting that autogenerate=true, but my own command columns will give "" in itemcommand. I hope u got what I intended.

Thanks,

Veena
0
Accepted
Princy
Top achievements
Rank 1
answered on 12 Jun 2013, 09:53 AM
Hi,
I have tried an example using GridButtonColumn,to delete the selected row on that button click.Try if this helps.

C#:
protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
        {
            GridButtonColumn buttonColumn;
            buttonColumn = new GridButtonColumn();
            RadGrid1.MasterTableView.Columns.Add(buttonColumn);
            buttonColumn.CommandName = "Delete";
            buttonColumn.Text = "Delete";
            buttonColumn.UniqueName = "Delete";             
        }
  }
 
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            string CustomerID = edit.GetDataKeyValue("CustomerID").ToString();
            conn.Open();
            string query = "DELETE from Customers Where CustomerID = '" + CustomerID + "'";
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }

Thanks,
Princy

0
Veena
Top achievements
Rank 1
answered on 12 Jun 2013, 10:28 AM

Hi Princy,

Thanks for your help. The problem with my column adding was that I was writing the line
RadGrid1.MasterTableView.Columns.Add(buttonColumn);
after the lines
buttonColumn.CommandName = "Delete";

buttonColumn.Text = "Delete";
buttonColumn.UniqueName = "Delete";       
When changed the order as in your code, the problem solved!. the button persists after postback also.

Thank you very much for your help once again,

Veena     
Tags
Grid
Asked by
Veena
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Veena
Top achievements
Rank 1
Share this question
or