在数据网格中显示数据 [英] Displaying data in a datagrid

查看:62
本文介绍了在数据网格中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


想要在gridview中显示我通过使用不带数据绑定的数据表在文本框中输入的数据

并且我在asp.net应用程序中采取了以下控制措施

Hello

i
want to display a data in gridview what i have entered in textboxes by using data table not with databinding

and i have taken the following control in asp.net application

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

       <br />
       <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" Text="Button" />
      
       <asp:GridView ID="GridView1" runat="server">
       </asp:GridView>
       

   </div>
   </form>



在按钮单击事件中,我添加了以下代码



and in button click event i have add the following code

DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Address");
        dt.Columns.Add("Password");
        DataRow dr = dt.NewRow();
        dr["Name"] = TextBox1.Text;
        dr["Address"] = TextBox2.Text;
        dr["Password"] = TextBox3.Text;
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();


然后,当我运行asp.net应用程序时,然后在文本框中输入以下信息,然后单击按钮

以下记录将添加到griview中,但问题是
当我输入下一个记录时,前一个记录不可见,新记录将可见

因此,请帮帮我


Then when i run the asp.net application then when i entered the followning information in textboxes then i click button

the following record will added in griview but the problem is that
when i enter next record the previous record was not visible the new record will be visible

so help me out

推荐答案

您将必须在Session中维护数据表.然后每次将新记录添加到会话中的数据表中.

当您要将数据表绑定到网格时,请从会话中拉出数据表.

我的另一个建议是,数据绑定逻辑应该在Page_load事件中而不是在click事件中.
You will have to maintain the datatable in the Session. And then add the new records to the datatable in the session every time.

When you want to bind the datatable to the grid, then pull the datatable from the session.

My other suggestion is that the databind logic should be in Page_load event rather than the click event


之所以会这样,是因为每次单击按钮时都在创建DataSet/DataTable的新实例.
在类级别声明一个DataSet/DataTable并在构造函数中创建其实例.
现在将您的行添加到表中并将其绑定到gridview,它将起作用.

另一种方法是单击按钮,将gridview的DataSource属性转换为DataTable并向该DataTable添加行并将其绑定回gridview.

请让我知道是否需要更多说明.
This is happening because you are creating new instance of DataSet/DataTable everytime when you click button.
Declare a DataSet/DataTable at Class Level and create its instance in constructor.
Now add your rows to the table and bind it to gridview, it''ll work.

Other method is on button click, cast the DataSource property of gridview into a DataTable and add row to that DataTable and bind it back to gridview.

Please let me know if you need more explanation.


使用ViewState保存数据表,它存储在客户端.

Use ViewState to save datatable, it store on client side.

DataTable dt = new DataTable();
if(dt == null)
{
   dt.Columns.Add("Name");
   dt.Columns.Add("Address");
   dt.Columns.Add("Password");
   ViewState("Dt") = dt;
}
else
{
   dt = (DataTable)ViewState("Dt");
   DataRow dr = dt.NewRow();
   dr["Name"] = TextBox1.Text;
   dr["Address"] = TextBox2.Text;
   dr["Password"] = TextBox3.Text;
   dt.Rows.Add(dr);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}


这篇关于在数据网格中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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