简体   繁体   English

如何使用动态控件创建可编辑的GridView

[英]How to create editable GridView with dynamic controls

How do I create editable gridviews with dynamic controls. 如何使用动态控件创建可编辑的网格视图。 The following image gives an idea. 下图给出了一个想法。

在此处输入图片说明

On page load, a gridview will be shown with only a single row other than the header. 在页面加载时,gridview将仅显示标题以外的一行。 The row contains two dropdownlist, two textboxes and an image button. 该行包含两个下拉列表,两个文本框和一个图像按钮。 Once I enter data in the first row, and then press the image button, a new row with these controls will be created, and so on. 在第一行中输入数据,然后按图像按钮后,将创建带有这些控件的新行,依此类推。

How is this possible? 这怎么可能?

Gridview by default does not provide a simple way of handling the Insert operations. 默认情况下,Gridview不提供处理插入操作的简单方法。 You will have to perform several steps to handle this. 您将必须执行几个步骤来处理此问题。

First of all each gridview column has to be converted to TemplateColumn(TemplateField). 首先,每个gridview列都必须转换为TemplateColumn(TemplateField)。 Then in the footer template you have to insert your controls(dropdown, textbox etc.) 然后,在页脚模板中,您必须插入控件(下拉菜单,文本框等)

<asp:TemplateField HeaderText="Item Category">
    <%--Define item template--%>
    <FooterTemplate>    
      <asp:DropDownList ID="dropDownListCategory" runat="server"></asp:DropDownList>
    </FooterTemplate>
</asp:TemplateField>
...... similarly other footer templates

You have to add the onclick Event for the ImageButton. 您必须为ImageButton添加onclick事件。

protected void imageButtonInsert_Click(object sender, EventArgs e)
{
    //Get the gridview row
    GridViewRow row = (sender as Control).NamingContainer as GridViewRow;
    DropDownList dropDownListCategory = row.FindControl("dropDownListCategory") as DropDownList;
    ///Similarly you can access the other controls here

    //If you are using SqlDataSource then you can add/assign these values to the insert parameters
    //Note that, you need to have insertcommand defined for the sql data source with appropreate parameters
    SqlDataSource1.InsertParameters.Add("category", dropDownListCategory.SelectedValue);
    //Similarly assign the other parameter values

    //Call the insert method of the sql data source.
    SqlDataSource1.Insert();

    //If you are not using data source approach, here you can insert the data to 
    // database by calling sql command or other ways


    //Rebind the gridview.
    GridView1.DataBind();

}

This should work, when you have some rows to display in the gridview. 当您在gridview中有一些行要显示时,这应该起作用。

BIG PROBLEM : EMPTY ROWS 大问题:空行

The above approach works when you have at least one row in the gridview. 当您在gridview中至少有一行时,上述方法有效。 But if you do not have any rows, that is for the first time, the gridview will be empty. 但是,如果您没有任何行,那就是第一次,gridview将为空。 If the gridview data source is empty, then it does not display anything, even header,footer, rows etc. As a initial workaround, first you need to make the header is always visible 如果gridview数据源为空,则它不显示任何内容,甚至标头,页脚,行等。作为初始解决方法,首先需要使标头始终可见

ShowHeaderWhenEmpty="true"  ShowFooter="true"

This makes sure that you have the headers all the time. 这样可以确保您一直都有标题。 Now footer will not at all display unless and until there is some row in the gridview. 现在,除非并且直到gridview中有一行,否则页脚将根本不会显示。 As a work around you can use EmptyDataTemplate of the gridview. 解决方法是,可以使用gridview的EmptyDataTemplate。

Here you need to add all your controls again, dropdownlist, textboxes etc. Not that you have to use here to make the same layout. 在这里,您需要再次添加所有控件,下拉列表,文本框等。不必在这里使用它来进行相同的布局。 You can use the same control ids, so that you dont need to write the codebehind again 您可以使用相同的控件ID,因此您无需再次编写代码

<EmptyDataTemplate>
    <tr>
        <td><asp:DropDownList ID="dropDownListCategory" runat="server"></asp:DropDownList></td>
        <td><asp:Button runat="server" CommandName="Insert"  ID="buttonInsert" OnClick="imageButtonInsert_Click" Text="Insert" /></td>
        <%--Other controls go here--%>
    </tr>
</EmptyDataTemplate>

This is now good for both first time and successive load. 现在,这对于首次加载和连续加载都是有利的。

The next question you may have is how to bind these dropdowns. 您可能遇到的下一个问题是如何绑定这些下拉菜单。 For this you need to use the RowCreated event, in which you can access the dropdowns and fill them 为此,您需要使用RowCreated事件,您可以在其中访问下拉列表并填充它们

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer || e.Row.RowType == DataControlRowType.EmptyDataRow)
    {
        DropDownList dropDownListCategory = e.Row.FindControl("dropDownListCategory") as DropDownList;
        //similarly access the other controls and bind them
    }
}

For Doing this use can use Dynamic table at client side. 为此,可以在客户端使用动态表。 where you can create whole table dynamically and also apply styling that can look like grid view. 您可以在其中动态创建整个表格,还可以应用看起来像网格视图的样式。 for Styling use can use JQuery DataTables which provice much more controls. 供样式使用的JQuery DataTables可以提供更多控件。

And also by using JQuery DataTables or JQuery Code you can also add rows dynamically at client side. 而且,通过使用JQuery DataTables或JQuery Code,您还可以在客户端动态添加行。

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

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