简体   繁体   English

动态添加按钮到网格视图

[英]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. 第二个问题现在我能够获得每一行的按钮,但使用下面的代码,我得到列的计数等于1.它实际上只读取html中添加的静态列而不是动态生成的静态列。

 //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. 这是关于如何在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: 在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. 确保您的WorkList声明的属性存在于您的代码中。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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