GridView中的问题newrow [英] Problem newrow in GridView

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

问题描述

创建新行时遇到问题.我有GridView 3列:
DropDownList(列名称:ProductName),文本框(名称列:Number),TextBox(列名称:value)和按钮字段",以使用CommandName ="addrow"添加新行
页面加载时,我将数据绑定到DropDownList.
用户将在DropDownList中选择产品名称,然后在文本框中的价格,数量列中输入值.
单击LinkBut​​tonField之后,新行将添加到GridView.
但是上一行的值未存储.
因为for循环重新创建上一行的控件(Dropdownlist,TextBox.
如何将前一行的值不丢失?
我的代码:

I have problems when creating new rows.I have a GridView 3 columns:
DropDownList (column name: ProductName), textbox (name column: Number), TextBox (column name: value) and Button Field to add new row with CommandName="addrow"
when the page loads,I bind Data into DropDownList.
The user will select the product name in DropDownList and enter the value into the textbox in the column named price,quantity.
After clicking LinkButtonField,a new row is added to the GridView.
But the value of previous row not stored.
Because for loop re-create controls of previous row(Dropdownlist,TextBox.
How to value of previous row not lost?
My code:

//Product class
clsProduct p=new clsProduct();
DataTable dt; 
void LoadData()
{
        dt = //get data from table Product(idPro,ProName,.....)
        GridView2.DataSource = tbl;//table containt a row
        GridView2.DataBind();
        //Problem is here
        for (int i= 0; i < GridView2.Rows.Count; i++)
        {
            //Find DropDownList ProductName
            DropDownList cbo = (DropDownList)GridView2.Rows[cborow].Cells[0].FindControl("DrdProName");
            cbo.DataSource = dt;
            cbo.DataValueField = "idPro";
            cbo.DataTextField = "TProName";
            cbo.DataBind();
}


为GridView制作表格:


Make table for GridView:

 void MakeTableGridView()
{
        if (tbl.Rows.Count == 0 || tbl==null)
        {
            tbl.Columns.Add("idPro", typeof(string));
            tbl.Columns.Add("quantity", typeof(int));
            tbl.Columns.Add("price", typeof(int));
            RowNew();
        }
}


新行:


New row:

void RowNew()
    {

        dr = tbl.NewRow();
        dr[0] = "";
        dr[1] = "1";
        dr[2] = "0";
        tbl.AcceptChanges();
        tbl.Rows.Add(dr);
    }


单击AddRow LinkBut​​tonField时的事件:


Event when click AddRow LinkButtonField:

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
        string cmd = e.CommandName;
        if (cmd =="addrow")
        {
            //Add New row in datatable
            RowNew();
            LoadData();
        }
}

推荐答案

为什么不使用可编辑的gridview?

您可以使用可编辑的gridview,它将自动处理所有内容.

这个例子对您真的很有帮助,

[
why are you not using editable gridview ?

you can use editable gridview and it will handeling everthing automatically.

this example can really helpful for you,

[Editable GridView in ASP.NET 2.0]


这篇关于GridView中的问题newrow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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