2013-03-11 123 views
1

我有一个gridview,我试图提取数据来填充3个文本框......我已经尝试了很多方法,但我无法达到正确的方式这...我已经手动构建了gridview,因此它没有“editTemplate”...有人能指导我以正确的方式做到这一点吗? 这是我的GridView的样子:从gridview提取数据到文本框

<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> 

那么这是怎么了,我添加了必要的列到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. 
    } 

我知道我做错事的我如何努力实现提取数据出来gridvie的w填充文本框,但不知道如何....(我仍然是一个初级编程,基本上是第一次做我在这里试图达到的目标)......

+0

尝试使用for循环之后,如果在您的代码完成,如我在我的答案中使用。 – Freelancer 2013-03-11 08:02:55

+1

您可以在绑定之前缓存您的dataTable,并使用缓存的dt检索数据。 – 2013-03-11 08:04:09

+0

请看我编辑的答案。第三选择 – Freelancer 2013-03-11 08:06:26

回答

1

使用以下代码>>

如果Windows应用程序>>

for(int i=0;i<gv.Rows.Count;i++) 
{ 
     id=gv.selectedRows[i].cells[0][0].value.toString(); 
     name=gv.selectedRows[i].cells[0][0].value.toString(); 
} 

从GridView控件这个否则

您可以获取所选行。

如果不想选择,那么,

for(int i=0;i<gv.Rows.Count;i++) 
    { 
      id=gv.Rows[i].cells[0][0].value.toString(); 
      name=gv.Rows[i].cells[0][0].value.toString(); 
    } 

第三种选择>>

 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]; 
    for(int i=0;i<GridView1.Rows.Count;i++) 
{ 
      Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text; 
      txtPart.Text = GridView1.SelectedRow[i][0].Cells[1].Text; 
      txtPart.Text = GridView1.Rows[i][0].Cells[2].Text; 
      Response.Write(gv.Rows[i][0].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column. 
     } 
} 

而且可以设置这些变量为文本框。

或者直接也可以设置它。

试用。

+0

谢谢你的回答!但是我得到一个错误“System.Web.UI.WebControls.GridView”没有包含“selectedRows”的定义,也没有扩展方法“selectedRows”接受类型为“System.Web.UI.WebControls”的第一个参数.GridView“可以找到(你是否缺少using指令或程序集引用?)”...我尝试过的方法之一出现此错误,但不知道如何解决它? – Kerieks 2013-03-11 07:40:36

+0

你有没有尝试过第二种选择? – Freelancer 2013-03-11 07:53:39

+0

是的,然后我得到一个错误,说:“System.Web.UI.WebControls.GridViewRow'不包含'行'的定义,并没有扩展方法'行'接受类型'System.Web的第一个参数。 UI.WebControls.GridViewRow'可以找到(你是否缺少使用指令或程序集引用?)“...试图寻找一个解决方案,但没有什么有用的......它可能是该控件是一个gridview然后使用数据表添加必要的列和行? – Kerieks 2013-03-11 07:59:38