下拉列表没有关于通过按钮单击在网格视图中添加新行的项目

时间:2014-03-22 23:08:30

标签: c# asp.net c#-4.0

我有一个网格视图,其中包含3个下拉列表和3个文本框以及一个按钮。我的下拉列表是从数据库中填充数据。当我在填充初始行后单击按钮添加新行时。它将第一行值设置为正确并添加一个新行。 但是新行在下拉列表中没有项目这是我的问题。其次当我再次点击按钮添加第三行时它甚至会丢失第一行填充的值并且同样的问题来了第一行,下拉列表中没有项目。

     void SetInitialRow()
    {

        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dt.Columns.Add(new DataColumn("Column6", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
        dr["Column6"] = string.Empty;
        dt.Rows.Add(dr);
        //dr = dt.NewRow();

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();


    }

     void AddNewRowToGrid()
    {

        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++)
                {
                    //extract the TextBox and dropdown lists values



                    DropDownList list1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList list2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                    DropDownList list3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("DropDownList3");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox3");


                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = list1.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = list2.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column4"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column5"] = list3.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["Column6"] = box3.Text;
                    Session["list1"] = dtCurrentTable.Rows[i - 1]["Column1"];
                    Session["list2"] =  dtCurrentTable.Rows[i - 1]["Column2"];
                    Session["list3"] = dtCurrentTable.Rows[i - 1]["Column5"];

                    rowIndex++;


                }

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

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();


            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks

        SetPreviousData();
    }
     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++)
                {

                    DropDownList list1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList list2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                    DropDownList list3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("DropDownList3");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox3");

list1.DataSource = bindddl();

                     list1.DataValueField = "Item_name";
                     list1.DataTextField = "Item_name";
                     list1.DataBind();
                     list1.Items.Insert(0, new ListItem("--Select--", "0"));

                     list2.DataSource = binddd2();
                     list2.DataValueField = "Sub_item";
                     list2.DataTextField = "Sub_item";
                     list2.DataBind();
                     list2.Items.Insert(0, new ListItem("--Select--", "0"));

                     list3.Items.Insert(0, new ListItem("--Select--", "0"));
                     list3.Items.Insert(1, new ListItem("Yes", "1"));
                     list3.Items.Insert(2, new ListItem("No", "2"));

                     box1.Text = dt.Rows[i]["Column3"].ToString();
                     box2.Text = dt.Rows[i]["Column4"].ToString();
                     box3.Text = dt.Rows[i]["Column6"].ToString();

                    list1.Text = Session["list1"].ToString();
                    list2.Text = Session["list2"].ToString();
                    box1.Text = dt.Rows[i]["Column3"].ToString();
                    box2.Text = dt.Rows[i]["Column4"].ToString();
                    list3.Text = Session["list3"].ToString();
                    box3.Text = dt.Rows[i]["Column6"].ToString();
                  //  Response.Write(Session["list1"]);



                    rowIndex++;


                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();

            Fillcombo();
            Fillcombo1();
            Fillcombo2();

        }

protected void ButtonAdd_Click(object sender,EventArgs e)         {

        AddNewRowToGrid();

        Fillcombo();
        Fillcombo1();
        Fillcombo2();


    }

Fillcombo,Fillcombo1是正在填充的功能下拉列表1,2到数据库Fillcombo2正在填充它有两个项目是&#39;并且没有&#39;。 我的Fillcombo功能就像这样

                try
        {
            string connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            using (var conn = new SqlConnection(connString))
            using (var cmd = conn.CreateCommand())
            {conn.Open();
                SqlCommand cmdstr = new SqlCommand("SELECT    [Sub_item]  FROM [ADS].[dbo].[Accounts_heads_data]", conn);
                // SqlCommand cmdstr = new SqlCommand("SELECT * FROM [ADS].[dbo].[Accounts_heads_data]  ", conn);
                SqlDataAdapter da2 = new SqlDataAdapter(cmdstr);


                DataSet ds2 = new DataSet();


                da2.Fill(ds2);

                DropDownList DropDownList2 = (DropDownList)Gridview1.Rows[0].FindControl("DropDownList2");



                DropDownList2.DataValueField = ds2.Tables[0].Columns["Sub_item"].ToString();
                DropDownList2.DataTextField = ds2.Tables[0].Columns["Sub_item"].ToString();
                DropDownList2.DataSource = ds2.Tables[0];
                DropDownList2.DataBind();
                DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));

                ViewState["fc1"] = ds2;
                conn.Close();
            }
        }

        catch (Exception ex)
        {
            Label1.Visible = true;
        }
    }

2 个答案:

答案 0 :(得分:1)

通过comenting检查 SetPreviousData();来自 AddNewRowToGrid函数

OR

在AddNewRowToGrid函数中只为新行设置数据并调用SetPreviousData();在AddNewRowToGrid函数的第一行中运行。

答案 1 :(得分:0)

我用一些测试数据编译了你的代码并稍微改了一下代码。请尝试以下内容并查看。

   void AddNewRowToGrid()
    {
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                for (int i = 0; i < dtCurrentTable.Rows.Count-1; i++)
                {
                    DropDownList list1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList1");
                    DropDownList list2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList2");
                    TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("TextBox2");
                    DropDownList list3 = (DropDownList)Gridview1.Rows[i].Cells[5].FindControl("DropDownList3");
                    TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[6].FindControl("TextBox3");

                    dtCurrentTable.Rows[i]["Column1"] = list1.SelectedItem.Text;
                    dtCurrentTable.Rows[i]["Column2"] = list2.SelectedItem.Text;
                    dtCurrentTable.Rows[i]["Column3"] = box1.Text;
                    dtCurrentTable.Rows[i]["Column4"] = box2.Text;
                    dtCurrentTable.Rows[i]["Column5"] = list3.SelectedItem.Text;
                    dtCurrentTable.Rows[i]["Column6"] = box3.Text;
                }
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }


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++)
            {

                DropDownList list1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                DropDownList list2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox1");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
                DropDownList list3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("DropDownList3");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox3");

                Fillcombo();
                Fillcombo1();
                Fillcombo2();


                box1.Text = dt.Rows[i]["Column3"].ToString();
                box2.Text = dt.Rows[i]["Column4"].ToString();                    
                box3.Text = dt.Rows[i]["Column6"].ToString();

                    if (i < dt.Rows.Count - 1)
                    {
                        list1.ClearSelection();
                        list1.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;

                        list2.ClearSelection();
                        list2.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;

                        list3.ClearSelection();
                        list3.Items.FindByText(dt.Rows[i]["Column5"].ToString()).Selected = true;
                    }

                rowIndex++;
            }
        }
    }
 }

  protected void ButtonAdd_Click(object sender, EventArgs e)
  {
        AddNewRowToGrid();
  }