简体   繁体   中英

How to create button column in radgrid dynamically in asp.net?

How to create button column in radgrid dynamically in asp.net?

I have a radgrid in My Page. I want to create button column in radgrid dynamically. I create columns and set command name for them.When I click on one of item and go to ItemCommand event , command name is null (""). Why command name is empty?

I have a data table that contain 3 column.

all columns the first , dynamically generate.

private void createColumnToGrid(DataTable dtData)
    {
        radGridTicketByFlagList.DataSource = new string[] { };
        radGridTicketByFlagList.Columns.Clear();

        for (int i = 0; i < dtData.Columns.Count; i++)
        {
                GridButtonColumn col = new GridButtonColumn();
                col.ButtonType = GridButtonColumnType.LinkButton;
                col.CommandName = "Total";
                col.UniqueName = "TCFlag" + i;
                col.HeaderText = dtData.Columns[i].ToString();

                col.DataTextField = dtData.Columns[i].ToString();

                radGridTicketByFlagList.Columns.Add(col);

        }
    }

Then fill data source of radgrid.

     private void FillRptTicketByFlagMonthList()
    {
//........
         radGridTicketByFlagList.DataSource = new string[] { };
         DataTable dtTicketFlag= clsReportManager.GetRptTicketByFlag(); // fill data table

         radGridTicketByFlagList.DataSource = dtTicketFlag;
         radGridTicketByFlagList.DataBind();
}

grid is generated. When I click on a item in grid (link button) , and go to ItemCommand event, e.CommandName is empty. Why??

I want to When I click on item , do an action for me.

Some time ago i had same problem, then i used this tip from telerik forum:

http://www.telerik.com/forums/commandname-is-empty-if-the-column-if-created-from-code

so sum up, you need to add to your grid Rebind() after creating button column.

Maybe there is some other way to make it works, but i don't know them :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM