数据表单文本到gridview而不使用datbase [英] Data Form text to gridview without using datbase

查看:46
本文介绍了数据表单文本到gridview而不使用datbase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表和两个文本框,我必须在点击按钮时将这些数据绑定到网格。这是我的代码......但是数据没有显示在网格中......但是调试数据会在相应的数据表中检索出来......有人可以帮助我吗















DataTable dt;

protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

dt = new DataTable();

dt.Columns.Add(Product,typeof(string));

dt.Columns.Add(Watt,typeof(string));

dt.Columns.Add(Duration,typeof(Int32));

GridView1.DataSource = dt;

GridView1.DataBind();

Session [dt] = dt;

}

}



protected void submit_Click(object sender,EventArgs e)

{

DataTable dt2 =(DataTable)Session [dt];

DataRow dr = dt2.NewRow();

dr [Product] = DropDownList1.Text;

dr [Watt] = Txtwatt.Text;

dr [Duration] = TxtDuration.Text;



dt2.Rows.Add(dr);

GridView1.DataSource = dt2;

GridView1.DataBind();

i am having a dropdownlist and two text box and i have to bind the data from these to a grid on a button click. here is my code ...but the data is not displaying in grid ...but on debugging data is retrieved on corresponding data table comns ...could some one help me out
?






DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("Product", typeof(string));
dt.Columns.Add("Watt", typeof(string));
dt.Columns.Add("Duration", typeof(Int32));
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dt"] = dt;
}
}

protected void submit_Click(object sender, EventArgs e)
{
DataTable dt2 = (DataTable)Session["dt"];
DataRow dr = dt2.NewRow();
dr["Product"] = DropDownList1.Text;
dr["Watt"] = Txtwatt.Text;
dr["Duration"] = TxtDuration.Text;

dt2.Rows.Add(dr);
GridView1.DataSource = dt2;
GridView1.DataBind();

推荐答案

DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if(Session["dt"]!=null)
{
  dt=(DataTable)Session["dt"];
}
else
{
  dt = new DataTable();
  dt.Columns.Add("Product", typeof(string));
  dt.Columns.Add("Watt", typeof(string));
  dt.Columns.Add("Duration", typeof(Int32));
}

GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void submit_Click(object sender, EventArgs e)
{
DataTable dt2 =new DataTable();
if(Session["dt"]!=null)
{
  dt2=(DataTable)Session["dt"];
}
DataRow dr = dt2.NewRow();
dr["Product"] = DropDownList1.Text;
dr["Watt"] = Txtwatt.Text;
dr["Duration"] = TxtDuration.Text;

dt2.Rows.Add(dr);
Session["dt"]=dt2;

}


这是一个样本,请根据您的要求试试



Its a sample , try this as per your requirement

   DataTable dtList = new DataTable();
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           dtList.Columns.Add("Name");

           ViewState["Data"] = dtList;
       }

   }


 void SaveAndBindDatatable(DataTable dt)
    {
        ViewState["Data"] = dtList;
        GridView1.DataSource = dtList;
        GridView1.DataBind();
    }
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
        dtList = (DataTable)ViewState["Data"];
        dtList.Rows.Add(txtValue.Text.Trim());
        SaveAndBindDatatable(dtList);
}





或者按照这个提示我使用datalist而不是gridview来制作自定义的自动建议控件。 />
GMail / Hotmail中的自动建议控制/ Facebook [ ^ ]


这篇关于数据表单文本到gridview而不使用datbase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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