在网格顶部添加新行

时间:2014-03-06 05:07:23

标签: c# asp.net gridview

我有一个gridview thad在网格的末尾添加了一个新行... doin的代码是

protected void addRow(object sender, EventArgs e)
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount");
                    DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr");
                    DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account");
                    DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit");
                    DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept");
                    DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate");
                    DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem");
                    TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");

                    drCurrentRow = dtCurrentTable.NewRow();

                    dtCurrentTable.Rows[i - 1]["amount"] = amount.Text.Trim() == "" ? 0 : Convert.ToDecimal(amount.Text);
                    dtCurrentTable.Rows[i - 1]["DrCr"] = DrCr.Text;
                    dtCurrentTable.Rows[i - 1]["account"] = account.Text;
                    dtCurrentTable.Rows[i - 1]["oprUnit"] = oprUnit.Text;
                    dtCurrentTable.Rows[i - 1]["dept"] = dept.Text;
                    dtCurrentTable.Rows[i - 1]["affiliate"] = affiliate.Text;
                    dtCurrentTable.Rows[i - 1]["openItem"] = openItem.Text;
                    dtCurrentTable.Rows[i - 1]["lineNar"] = lineNar.Text;
                    rowIndex++;
                }

                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Grid_AccEntry.DataSource = dtCurrentTable;
                Grid_AccEntry.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }




    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount");
                    DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr");
                    DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account");
                    DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit");
                    DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept");
                    DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate");
                    DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem");
                    TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");

                    amount.Text = dt.Rows[i]["amount"].ToString();
                    DrCr.Text = dt.Rows[i]["DrCr"].ToString();
                    account.Text = dt.Rows[i]["account"].ToString();
                    oprUnit.Text = dt.Rows[i]["oprUnit"].ToString();
                    dept.Text = dt.Rows[i]["dept"].ToString();
                    affiliate.Text = dt.Rows[i]["affiliate"].ToString();
                    openItem.Text = dt.Rows[i]["openItem"].ToString();
                    lineNar.Text = dt.Rows[i]["lineNar"].ToString();

                    //to bind long narr text to gridview line narr
                    TextBox lineNar1 = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");
                    if (lineNar1.Text == "")
                        lineNar1.Text = lngNar.Text;

                    rowIndex++;
                }
            }
        }
    }

这会在网格末尾添加新行....但我想在顶部添加新行...我必须对此代码进行哪些更改 < / p>

2 个答案:

答案 0 :(得分:3)

您的代码在网格末尾添加了新行,因为您在DataTable的最后一个索引处添加了新行。而不是你应该在 0th index 。您可以使用 InsertAt 方法,如下例所示:

var dtCurrentTable = (DataTable)ViewState["CurrentTable"];
var newRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows.InsertAt(newRow, 0);

答案 1 :(得分:0)

您可以简单地使用Insert方法在Gridview的顶部插入行,只需实现此方法

  

插入(索引:数字,项目:任意):无效

示例:

gridview1.Rows.Insert(0,item);