我有一个网格视图,我正在尝试提取数据以填充 3 个文本框......我尝试了很多方法,但我无法找到正确的方法......我已经建立了手动gridview,因此它没有“editTemplate”......有人可以指导我以正确的方式做到这一点吗?这就是我的网格视图的样子:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
EmptyDataText="No parts has been added to the model, please add a part."
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px"
CellPadding="3" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server" CssClass="EditButton" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="DeleteButton" runat="server" CssClass="DeleteButton" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid"
BorderWidth="2px" />
</asp:GridView>
然后这就是我将必要的列添加到该网格视图的方式:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
EmptyDataString();
}
else
{
dt = (DataTable)ViewState["DataTable"];
}
ViewState["DataTable"] = dt;
}
#region GridView
private void EmptyDataString()
{
TemplateBuilder tmpEmptyDataTemplate = new TemplateBuilder();
tmpEmptyDataTemplate.AppendLiteralString("No parts has been added to the model, please add a part.");
GridView1.EmptyDataTemplate = tmpEmptyDataTemplate;
GridView1.DataBind();
}
private void MakeDataTable()
{
dt.Columns.Add("Item");
dt.Columns.Add("Quantity");
dt.Columns.Add("Price P/Quantity");
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr["Item"] = txtPart.Text;
dr["Quantity"] = numQuantity.Text;
dr["Price P/Quantity"] = txtPricePQ.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnAddPart_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
ClearNewParts();
}
#endregion
这些是我尝试过的一些方法:(当我转向下一种方法时,所有这些都被注释掉了.....
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Edit")
{
if (GridView1.SelectedRow != null)
{
//dataTable.Rows[GridView1.SelectedRow].BeginEdit();
//dataTable.Rows[GridView1.SelectedRow]["yourFieldName"] = newValue;
//dataTable.Rows[GridView1.SelectedRow].EndEdit();
//gridView.DataSource = dataTable;
}
// GridViewRow row = GridView1.Rows[e.RowIndex];
// Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;
// txtPart.Text = GridView1.SelectedRow.Cells[1].Text;
// txtPart.Text = GridView1.Rows[index].Cells[2].Text;
// Response.Write(gv.Rows[index].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
}
我知道我在尝试如何从gridview中提取数据以填充文本框时做错了,但不确定如何....(我仍然是初级编程人员,基本上是第一次做我'我试图在这里实现)....