在gridview(Web应用程序)中添加数据C#

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在gridview(Web应用程序)中添加数据C#相关的知识,希望对你有一定的参考价值。

我在我的网络应用程序中有gridview

此网格视图应显示3列(表,列,new_columns)

我想在click事件中显示网格视图,我想在第一个Field(表格)中绑定所有表格,并在gridview内的下拉列表中绑定每个表格的所有相应列,并在第二个下拉列表中绑定所有新列

这是代码

        DataTable test = new DataTable();
        DataTable _dbtest = new DataTable();
        list_of_table_of_old_database = db.Select("", "", "", "", "", "", "", "", "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '" + old_database.Text.Trim() + "' ", "");
        list_of_table_of_current_database = db.Select("", "", "", "", "", "", "", "", "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '" + new_database.Text.Trim() + "' ", "");

        DataView view = new DataView(list_of_table_of_old_database);


        DataTable dtTable = new DataTable();


        test = db.Select("", "", "", "", "", "", "", "", "select  distinct k.table1 from (SELECT TABLE_NAME as table1 FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '" + old_database.Text.Trim() + "') as k INNER JOIN (SELECT TABLE_NAME as table2 FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '" + new_database.Text.Trim() + "')as a;", "");




        DropDownList3.Items.Clear();
        DropDownList3.Items.Insert(0, "--select--");

        DataTable dt = new DataTable();
        for (int i = 0; i < test.Rows.Count; i++)
            {

                _dbtest = db.Select("", "", "", "", "", "", "", "", "select a.fieldname from (SELECT COLUMN_NAME as fieldname FROM INFORMATION_SCHEMA.columns WHERE TABLE_SCHEMA = '" + old_database.Text.Trim() + "' and TABLE_NAME =  '" + test.Rows[i][0].ToString() + "') as A LEFT JOIN (SELECT COLUMN_NAME as fieldname FROM INFORMATION_SCHEMA.columns WHERE TABLE_SCHEMA = '" + new_database.Text.Trim() + "' and TABLE_NAME =  '" + test.Rows[i][0].ToString() + "') B on (A.fieldname = B.fieldname) where B.fieldname is null ", "");

                if (_dbtest.Rows.Count > 0)
                {

                    //here i want to display all tables in the gridview row by row (test.Rows[i][0].ToString())


                } 

                  // and i want to bind all columns (_dbtest) of each tables inside the loop in first dropdownlist in gridview 

                 // and same  columns (_dbtest) of each tables inside the loop in second dropdownlist in gridview
            }

我想在运行循环时找到一些在gridview中绑定数据的方法,因此我可以为每个字段提供正确的数据

这是html代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        Height="237px" Width="632px" onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField FooterText="tables" HeaderText="tables">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField FooterText="columns" HeaderText="columns">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlcolumns" runat="server" 
                         Height="16px" Width="181px">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField FooterText="vms-columns" HeaderText="vms-columns">
                <ItemTemplate>
                    <asp:DropDownList ID="ddl_vms_columns" runat="server" 
                         Height="20px" Width="153px">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

所以最后,这个网格视图将显示网格视图中的所有表格以及该表格的每一列

答案

第一步是加载所有表并将其绑定到gridView。你不会使用事件GridView1_RowDataBound来做到这一点。

加载表后,可以使用事件GridView1_RowDataBound填充DropDownLists。您可以找到一些示例来解释如何执行此操作:

https://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx

https://codepedia.info/bind-dropdownlist-inside-gridview-edit-template/

以上是关于在gridview(Web应用程序)中添加数据C#的主要内容,如果未能解决你的问题,请参考以下文章

删除 - (不间断空格) - 以某种方式添加到 GridView 的绑定值中

asp.net VS2008 GridView 里面怎么排序

在 asp.net Web 应用程序的 Gridview 中使用 shift + 滚动进行水平滚动?

ASP.net GridView 的小问题

无法将日期选择器添加到 C# .NET gridview 单元格

vs2005(c#.net2.0)中用gridview查询数据库问题,高手请进