简体   繁体   中英

Dynamically adding button to grid view

My scenario is that i am creating a data table dynamically and adding the latter to a data set and displaying it in a grid view. I want to add a button "Add to chart" at the end of each row which will have additional functionality.

My code for creating the dynamic data table :

    try
        {


            response = client.query(a);

            ///List of fields
            var fields = response.@return.fields;

            //loop through each column
            foreach (String column in fields)
            {
                dtservice.Columns.Add(column);

            }

            ///List of value return as list of object
            var values = response.@return.values.ToList();


            ///get the first object from the list of object
            foreach (object item in values)
            {
                if (item == null) continue;

                foreach (PropertyInfo property in item.GetType().GetProperties())
                {

                    // do something with the property 
                    List<string> valueList = (List<string>)(property.GetValue(item, null));
                    dtservice.Rows.Add(valueList.ToArray());


                }
            }


        }
        catch (Exception error)
        {

            var b = error.ToString();
        }

        //create dataset
        DataSet test= new DataSet();

        test.Tables.Add(dtservice);

        return test;


    }

I have tried using the below code but the button dissapear on click.

    protected void WorkList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // CHECK IF ROW IS NOT IN EDIT MODE
        if (e.Row.RowType == DataControlRowType.DataRow)

        {
            // CREATE A Button
            Button btn = new Button();
            btn.ID = "btnEstados";
            btn.Text = "Estados";

            // ADD BUTTON TO EACH ROW IN 2ND COLUMN
            e.Row.Cells[7].Controls.Add(btn);
        }
    }

second problem now i was able to get the button on each row, but using the code below,i get the count of column equal to 1. It is actually reading only the static column added in the html and not the one generated dynamically.

 //adding column to datatable
        for (int row = 0; row < test.Columns.Count - 1; row++)
        {

            ServiceName.Columns.Add(test.HeaderRow.Cells[row].Text, typeof(string));

        }

This is the other way on how to add button in Gridview.

<asp:GridView ID="WorkList" runat="server" Width="100%" AutoGenerateColumns="false" AllowPaging="false" AllowSorting="false"
                            EmptyDataText="No Record Found"
                            OnRowCreated="WorkList_RowCreated"
                            OnRowCommand="WorkList_RowCommand"
                            OnRowCancelingEdit="WorkList_RowCancelingEdit"
                            OnRowEditing="WorkList_RowEditing">

                            <AlternatingRowStyle CssClass="alt" BackColor="#CCFF99"></AlternatingRowStyle>
                            <Columns>

                                <asp:ButtonField ButtonType="Button" CommandName="Estados" HeaderText="" ShowHeader="True" Text="Estados" ItemStyle-Width="30px" />
                            </Columns>
                            <HeaderStyle BackColor="#808080" ForeColor="#48D1CC" />

                        </asp:GridView>

In WorkList_RowCommand:

int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = WorkList.Rows[index];
WorkList.Rows[index];
 if (e.CommandName == "Estados")
 {
      //your code here like `gvRow.Cells[0].Text`
 }

Make sure the property declared of your WorkList is exists in your code behind.

Hope it helps.

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