2

我有一个网格视图

<asp:GridView ID="gvOrders" runat="server">
            <Columns>
                <asp:BoundField DataField="Item" HeaderText="Item" SortExpression="Item" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField ID="hdnItemID" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />

            </Columns>
        </asp:GridView>

...................................................

现在我想使用从 Textbox/DropdownList 获取的数据将行添加到 gridview。然后如何访问这个 GridData 行。

4

2 回答 2

0

假设您的 GridView 数据源是一个名为 Dtable 的 DataTable;

DataRow row = new DataRow();             

row["columnName"] = textBox1.Text; 

DTable.Rows.Add(row);

gvOrders.DataSource=Dtable;
gvOrders.DataBind();
于 2013-04-27T07:30:07.530 回答
0

请试试这个,它将如何帮助你。

页面:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>

                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>                        
                    <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number"></asp:BoundField>                        
                </Columns>

  </asp:GridView>

C#代码:

DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dt = new DataTable();
        MakeDataTable();
    }
    else
    {
        dt = (DataTable)ViewState["DataTable"];
    }
    ViewState["DataTable"] = dt;
}

private void MakeDataTable()
{
    dt.Columns.Add("Name");
    dt.Columns.Add("Number");
}

protected void Button2_Click(object sender, EventArgs e)
{
    AddToDataTable();
    BindGrid();
}

private void AddToDataTable()
{
    DataRow dr = dt.NewRow();
    dr["Name"] = txtName.Text;
    dr["Number"] = txtNumber.Text;
    dt.Rows.Add(dr);
}

private void BindGrid()
{
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
于 2014-04-03T10:58:19.523 回答