我有一个gridview
<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行。
答案 0 :(得分:0)
假设您的GridView数据源是名为Dtable的DataTable;
DataRow row = new DataRow();
row["columnName"] = textBox1.Text;
DTable.Rows.Add(row);
gvOrders.DataSource=Dtable;
gvOrders.DataBind();
答案 1 :(得分:0)
请试试这个,它会对你有什么帮助。
Aspx Page:
<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();
}