我想在gridview中显示数据,而不是通过数据绑定使用数据表在文本框中输入 [英] i want to display a data in gridview what i have entered in textboxes by using data table not with databinding

查看:70
本文介绍了我想在gridview中显示数据,而不是通过数据绑定使用数据表在文本框中输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在gridview中显示我通过使用datatable在文本框中输入的数据

我在源代码部分添加了以下内容

i want to display a data in gridview what i have enter in textboxes by using datatable

i have add the following in source part

<form id="form1" runat="server">
  <div>

      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
      <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click"

          style="height: 26px" Text="Button" />
      <br />
      <br />
      <br />
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
      <br />

  </div>
  </form>


在按钮中单击事件

我已经写了以下代码


in button click event

i have write the following code

{
        DataTable dt = new DataTable(); 
        DataRow dr; 
        dt.Columns.Add("Name");
        dt.Columns.Add("Address"); 
        dt.Columns.Add("Number"); 
        //First fill all the date present in the grid 
        for(int intCnt=0;intCnt<gridview1.rows.count;intcnt++)>
                { 
            if (GridView1.Rows[intCnt].RowType == DataControlRowType.DataRow)
            {
                dr = dt.NewRow();
                dr["Name"] =GridView1.Rows[intCnt].Cells[0].Value; 
                dr["Address"] = GridView1.Rows[intCnt].Cells[1].Value; 
                dr["Number"] = GridView1.Rows[intCnt].Cells[2].Value;
                dt.Rows.Add(dr);
            }
        }
        dr = dt.NewRow();
        dr["Name"] = TextBox1.Text;
        dr["Address"] = TextBox2.Text;
        dr["Number"] = TextBox3.Text;
        dt.Rows.Add(dr); 
       Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }


当我运行asp.net应用程序时

将会发生以下编译错误


when i run the asp.net application

the following compile error will occur

Compiler Error Message: CS1061: 'System.Web.UI.WebControls.TableCell' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Web.UI.WebControls.TableCell' could be found (are you missing a using directive or an assembly reference?)

推荐答案

尝试一下,

Try this,

protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = null;
        try
        {
            if (Session["dtItems"] != null)
            {
                dt = (DataTable)Session["dtItems"];
            }
            else
            {
                dt = new DataTable();
                dt.Columns.Add("Quantity");
                dt.Columns.Add("Rate_Unit");
                dt.Columns.Add("Amount");
            }
            DataRow dataRow;
            dataRow = dt.NewRow();
            dataRow["Quantity"] = TextBox1.Text;
            dataRow["Rate_Unit"] = TextBox2.Text;
            dataRow["Amount"] = TextBox3.Text;
            dt.Rows.Add(dataRow);
            dt.AcceptChanges();
            Session["dtItems"] = dt;
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }
    }


这篇关于我想在gridview中显示数据,而不是通过数据绑定使用数据表在文本框中输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆