有没有办法在pageLoad事件中显示空GridView?

时间:2015-12-29 08:17:14

标签: c# asp.net gridview

希望你们都很好

任何人都可以帮助我,我有一个网格视图,可以从数据库中获取记录,但显示我在网格中输入的所有先前数据,但现在我想显示我输入的所有数据,直到甚至调用的页面加载,当页面加载时不应显示所有输入的记录。 在此先感谢

这是我工作背后的代码

  protected void BindData()
        {
            SqlConnection conne = new SqlConnection("Data Source=192.168.0.65;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=mushko");

            DataSet ds = new DataSet();

            conne.Open();

            string cmdstr = "SELECT * FROM OPR1 ";

            SqlCommand cmd = new SqlCommand(cmdstr, conne);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);

            adp.Fill(ds);

            cmd.ExecuteNonQuery();

            conne.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();
         //   GridView1.DataSource =null;

           // GridView1.DataSource = ds;
          //  GridView1.DataBind();
            //ds = null;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SqlConnection conne = new SqlConnection("Data Source=192.168.0.65;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=mushko");

            conne.Open();

            if (e.CommandName.Equals("ADD"))
            {

                Calendar txtOpenDate = (Calendar)GridView1.FooterRow.FindControl("txtOpenDate");

                TextBox txtCloseDate = (TextBox)GridView1.FooterRow.FindControl("txtCloseDate");

                DropDownList DropDownListoppr = (DropDownList)GridView1.FooterRow.FindControl("DropDownListoppr");

                DropDownList DropDownListStages = (DropDownList)GridView1.FooterRow.FindControl("DropDownListStages");

                TextBox txtAddLine = (TextBox)GridView1.FooterRow.FindControl("txtAddLine");

                TextBox txtStages = (TextBox)GridView1.FooterRow.FindControl("txtStages");

                TextBox txtAddOppId = (TextBox)GridView1.FooterRow.FindControl("txtAddOppId");




                string cmdstr = "insert into OPR1(OpenDate,CloseDate,SlpCode,Step_Id,Line,OpprId) values(@txtOpenDate,@txtCloseDate,@SlpCode,@Step_Id,@txtAddLine,@txtAddOppId)";

                SqlCommand cmd = new SqlCommand(cmdstr, conne);

                cmd.Parameters.AddWithValue("@txtOpenDate", txtOpenDate.TodaysDate);

                cmd.Parameters.AddWithValue("@txtCloseDate", txtCloseDate.Text);


                cmd.Parameters.AddWithValue("@Step_Id", DropDownListStages.SelectedValue.ToString()); // SelectedItem.ToString());

                cmd.Parameters.AddWithValue("@SlpCode", DropDownListoppr.SelectedValue.ToString()); // SelectedItem.ToString());

                cmd.Parameters.AddWithValue("@txtStages", txtStages.Text);

                cmd.Parameters.AddWithValue("@txtAddLine", txtAddLine.Text);

                cmd.Parameters.AddWithValue("@txtAddOppId", txtAddOppId.Text);

                cmd.ExecuteNonQuery();
             //   this.TextBox1.Text = DropDownList1.SelectedItem.ToString();
           //     this.TextBox3.Text = DropDownList1.SelectedValue.ToString();


                BindData();
                conne.Close();
            }

        }
        protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Footer)
            {

                DropDownList DropDownListoppr = (DropDownList)e.Row.FindControl("DropDownListoppr");
                DropDownList DropDownListStages = (DropDownList)e.Row.FindControl("DropDownListStages");

                DataTable CardCode = new DataTable();
                DataTable CardCode1 = new DataTable();

                SqlConnection connection = new SqlConnection("Data Source=192.168.0.65;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=mushko");
                using (connection)
                {

                    // SqlCommand theCommand = new SqlCommand("select distinct T1.SlpCode,T1.SlpName,T3.StepId,T3.Descript from OSLP T1 left join OPR1 T2 on T1.SlpCode=T2.SlpCode right join OOST T3 on T3.StepId=t2.Step_Id ", connection);

                    SqlCommand theCommand = new SqlCommand("select SlpCode,SlpName from OSLP ", connection);
                    SqlCommand theCommand1 = new SqlCommand("select Distinct StepId, Descript from OOST ", connection);
                    SqlDataAdapter adapter = new SqlDataAdapter(theCommand);
                    SqlDataAdapter adapter1 = new SqlDataAdapter(theCommand1);

                    adapter.Fill(CardCode);
                    adapter1.Fill(CardCode1);
                    //DropDownList7.DataSource = CardCode;
                    //DropDownList7.DataTextField = "SlpName";
                    //DropDownList7.DataValueField = "SlpCode";
                    //DropDownList7.DataBind();

                    if (CardCode.Rows.Count > 0)
                    {
                        for (int i = 0; i < CardCode.Rows.Count; i++)
                        {
                            string name3 = CardCode.Rows[i]["SlpName"].ToString();
                            string slpCode = CardCode.Rows[i]["SlpCode"].ToString();
                            DropDownListoppr.Items.Add(new ListItem(name3, slpCode));



                        }
                    }

                    if (CardCode1.Rows.Count > 0)
                    {
                        for (int j = 0; j < CardCode1.Rows.Count; j++)
                        {
                            string name4 = CardCode1.Rows[j]["Descript"].ToString();
                            string stageCode = CardCode1.Rows[j]["StepId"].ToString();
                            DropDownListStages.Items.Add(new ListItem(name4, stageCode));
                        }
                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

如果要在页面加载事件中保持网格为空,则不要编写代码以在页面加载事件上绑定它,而是将代码写入&#34;添加按钮&#34;点击事件(如果那是你想要的)。

答案 1 :(得分:-1)

用于显示空记录网格您可以使用EmptyDataTemplate。

如需了解更多信息,可以试用Link