简体   繁体   English

如何在C#2.0中向具有列的网格视图添加行?

[英]How to add rows to a Grid View having columns in c# 2.0?

I am pretty much sure that this question have been asked and answered several times before. 我非常确定这个问题已经被问过并回答过几次了。 But I am asking it again for an alternate answer. 但是我再次要求它替代答案。 Here is my GridView 这是我的GridView

<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:10px auto auto 30px;width:500px;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="BillID" HeaderText="Bill ID" Visible="False" />
                <asp:BoundField DataField="SerialNo" HeaderText="Bill No" />
                <asp:BoundField DataField="BilledWeekNo" HeaderText="Billed Week" />
                <asp:BoundField DataField="BilledWeekDate" HeaderText="Billed Date" />
                <asp:BoundField DataField="Amount" HeaderText="Amount" />
                <asp:BoundField DataField="BillStatus" HeaderText="Bill Status" />
                <asp:CommandField SelectText="Print" ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView> 

Now I want to add rows to this gridview in runtime. 现在,我想在运行时向此gridview添加行。 In other articles, they suggest to define a DataTable , then ADD my desired COLUMNS to that table, then ADD rows in that TABLE and finally BIND the table to the GridView . 在其他文章中,他们建议定义一个DataTable ,然后将ADD my desired COLUMNS ADD rows in that TABLE到该表,然后ADD rows in that TABLE ,最后BIND the table to the GridView Yes, This solves the problem and I can also do that. 是的,这可以解决问题,我也可以做到。 But what I want to do is, as I already have columns added into my gridview, I just want to add rows in it. 但是我想做的是,因为我已经在gridview中添加了列,所以我只想在其中添加行。 and NOT Define a DataTable and then Bind it to GridView stuff . 而不是定义一个DataTable,然后将其绑定到GridView的东西 I tried below, 我在下面尝试过

oOutputBill = (clsBill[])oOutput;
            if (oOutputBill.Length > 0)
            {
                for (int i = 0; i < oOutputBill.Length; i++)
                {
                    GridViewRow oRow = new GridViewRow();
                    oRow.Cells[0].Text = Convert.ToString(oOutputBill[i].BillID);
                    oRow.Cells[1].Text = Convert.ToString(oOutputBill[i].SerialNo);
                    oRow.Cells[2].Text = Convert.ToString(oOutputBill[i].BilledWeekNo);
                    oRow.Cells[3].Text = Convert.ToString(oOutputBill[i].BilledWeekDate);
                    oRow.Cells[4].Text = Convert.ToString(oOutputBill[i].Amount);
                    oRow.Cells[5].Text = Convert.ToString(oOutputBill[i].BillStatus);
                }
            }

and as expected, it gives the error "No Overload of Method GridViewRow takes 0 arguments". 并出现预期的错误“方法GridViewRow的任何重载都不接受0个参数”。 How can I achieve this? 我该如何实现? Thanks in advance. 提前致谢。

This link may be helpful. 链接可能会有所帮助。

But I recommend to bind GridView by DataSource. 但是我建议按DataSource绑定GridView。 Just save your data into session and instead of manually adding rows into GridView, add new row into saved data and rebind your grid. 只需将数据保存到会话中,而不是手动将行添加到GridView中,而是将新行添加到已保存的数据中并重新绑定网格。

Example: 例:

// If you have List of clsBill.
List<clsBill> oOutputBill = 'Filled from DB...';

// Save Data into session
Session["oOutputBill"] = oOutputBill;

// Bind your GridView
dgvGeneralBillList.DataSource = Session["oOutputBill"];
dgvGeneralBillList.DataBind();

...

// Get saved data and insert new row
List<clsBill> oOutputBill = Session["oOutputBill"] as List<clsBill>;

if(oOutputBill != null)
{
    oOutputBill.Add(new clsBill() { /* Fill class properties */ } );

    // Rebind grid
    dgvGeneralBillList.DataSource = Session["oOutputBill"];
    dgvGeneralBillList.DataBind();
}

This is what I am using now as solution. 这就是我现在使用的解决方案。 My GridView is- 我的GridView是-

<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:10px auto auto 30px;width:auto;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="BillID" HeaderText="Bill ID" />
                <asp:BoundField DataField="SerialNo" HeaderText="Bill No" />
                <asp:BoundField DataField="BilledWeekNo" HeaderText="Billed Week" />
                <asp:BoundField DataField="BilledWeekDate" HeaderText="Billed Date" />
                <asp:BoundField DataField="Amount" HeaderText="Amount" />
                <asp:BoundField DataField="BillStatus" HeaderText="Bill Status" />
                <asp:CommandField SelectText="Print" ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

And the way of adding row is- 并且添加行的方式是-

oOutputBill = (clsBill[])oOutput;
            if (oOutputBill.Length > 0)
            {
                dgvGeneralBillList.DataSource = oOutputBill;
                dgvGeneralBillList.DataBind();
                dgvGeneralBillList.Visible = true;
            }

This answer is similar to define a table, add columns to table, add rows to table and finally bind the table to gridview but with less code. 这个答案类似于定义一个表,向表中添加列,向表中添加行,最后将表绑定到gridview,但是代码更少。 in this way, only columns defined in the gridview are bound with the data source. 这样,只有在gridview中定义的列才与数据源绑定。 Please note that, if you want to do something similar to OnSelectedIndexChanged and try to get any data from the gridview, the DataColumn HAVE TO be DEFINED and VISIBLE in the gridview. 请注意,如果你想要做类似的东西OnSelectedIndexChanged并尝试从GridView控件获得的数据,那么DataColumn的 HAVE在GridView中定义可见 But if anyone can provide me the sample code as my question, it would be highly appreciated. 但是,如果有人可以向我提供示例代码作为我的问题,将不胜感激。

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

相关问题 如何将数据绑定到C#Windows Forms网格视图中的列,这些列存储在SQL表的行中 - How to bind data to the columns in C# Windows Forms grid view, which are stored in rows in SQL table 如何在C#windows应用程序的数据网格视图中添加超过65,535列? - How to add more than 65,535 columns in a data grid view in C# windows application? C#XAML如何修改网格的行和列的尺寸 - C# XAML How to modify the dimensions of the rows and the columns of a Grid 如何在C#的网格视图中添加密码字段 - How to add password field in Grid View in c# 如何在C#中向数据网格视图添加数据源 - How to add data source to Data Grid View in C# 如何从combox向C#中的数据网格视图添加值 - How to Add a value from combox to data grid view in C# 从数据网格视图中选择多行并以逗号分隔的字符串格式添加到变量 C# - Select multiple rows from data grid view and add to a variable in a comma separated string format C# C# 数据网格视图,SQL Server:选择多行,然后将具有指定列的选定行插入到 SQL 表中 - C# Data grid view, SQL Server: Select multiple Rows then insert selected rows with specified columns into SQL table 如何将列动态添加到数据网格视图 - How to add columns dynamically to data grid view C# mysql 仅查看数据网格视图中的特定列 - C# mysql view only specific columns in data grid view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM