在4.0中向Gridview添加新行 [英] Adding new row to Gridview in 4.0

查看:83
本文介绍了在4.0中向Gridview添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



长期使用Codeproject感觉很好.

我的问题是要在网格视图中动态添加一行.

过去我做过Gridview.rows.add(bla bla).

现在,我已移至VS 2010和Framework 4.0.

奇怪的是,我找不到添加新行的方法Add().

智慧不会在"Gridview.Rows"之后给我Add().

确保我不会将gridview与任何数据源绑定.

只是想将行添加到gridview.

谢谢,
Hemant Thakar

Hi,

Its feeling good to be on Codeproject after a long time.

My issue is about adding a row to gridview dinamically.

In past I have done like Gridview.rows.add(bla bla).

Now I have moved to VS 2010 & Framework 4.0.

Its strange that I am not able to find method Add() for adding new row.

The intellisence doesn''t giving me Add() after "Gridview.Rows.".

Make sure I dont wont to bind gridview with any datasource.

Just want to add row to gridview.

Thanks,
Hemant Thakar

推荐答案

是的,您是正确的.不能直接将一行添加到gridview中,但是如果要在gridview中添加一行,可以添加在数据源(即数据表)中将其作为数据源,然后将其作为数据源提供给gridview.
Ya,you are right.A row can''t be added to gridview directly but if you want to add a row in gridview,you can add it in datasource i.e. datatable and then give it as a datasource to gridview.


请使用以下代码向网格中添加新行

Please use below code for adding a new row to grid

DataTable _dtBands = ((DataTable)ViewState["Data"]).Clone();

        DataRow row;
        if (_type == "ADD")
        {
            row = _dtBands.NewRow();
            row["Item_ID"] = "";
            row["Item_Name"] = "";
            row["UOM"] = "";
            row["UOM_id"] = "";
            row["Quantity"] = "0";
            row["Tolrence"] = "0";
            row["markup"] = "0";
            row["markup_Fixed"] = "0";

            _dtBands.Rows.Add(row);
            for (int i = 0; i < grdMain.Rows.Count; i++)
            {
                row = _dtBands.NewRow();
                row["Item_ID"] = ((DropDownList)grdMain.Rows[i].FindControl("ddlItemName")).SelectedValue.ToString();
                row["Item_Name"] = ((DropDownList)grdMain.Rows[i].FindControl("ddlItemName")).SelectedItem.Text;
                row["UOM"] = ((Label)grdMain.Rows[i].FindControl("lblUOM")).Text;
                row["UOM_id"] = ((HiddenField)grdMain.Rows[i].FindControl("hdnuomid")).Value;
                row["Quantity"] = ((TextBox)grdMain.Rows[i].FindControl("txtqty")).Text;
                row["Tolrence"] = ((TextBox)grdMain.Rows[i].FindControl("txttolrence")).Text;
                row["markup"] = ((TextBox)grdMain.Rows[i].FindControl("txtmarkup")).Text;
                row["markup_Fixed"] = ((TextBox)grdMain.Rows[i].FindControl("txtmarkupFixed")).Text;
                _dtBands.Rows.Add(row);
            }            
        }


grdMain.DataSource = _dtBands ;
grdMain.DataBind();


检查这些博客
http://forums.asp.net/t/1147158.aspx [ http://www.highoncoding.com/Articles/98_Adding_a_New_Row_in_GridView.aspx [
check these blogs
http://forums.asp.net/t/1147158.aspx[^]
http://www.highoncoding.com/Articles/98_Adding_a_New_Row_in_GridView.aspx[^]
--NDK


这篇关于在4.0中向Gridview添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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