GridView当我从数据库表中删除所有行时不显示

时间:2015-12-29 11:32:52

标签: c# mysql asp.net gridview

我面临一个问题不知道如何设置这个问题的问题是我的项目工作正常但是当我从sql databse中删除所有行时它不显示网格友好帮助 您的回复将受到高度赞赏

这是我的代码背后

public partial class Web_grid : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

            BindData();


        }
    }

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

            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.6;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=malick");

            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.6;Initial Catalog=TestDataBase;Persist Security Info=True;User ID=sa;Password=malick");
                using (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)

不需要cmd.ExecuteNonQuery();在BindData方法中,因为您没有执行任何插入,删除或更新操作。

答案 1 :(得分:0)

尝试将此属性添加到您的aspx gridview

EmptyDataText = “SomeText” 则会

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatext(v=vs.110).aspx

或者您可以使用EmptyDataTemplate - 就像TemplateField

一样

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate(v=vs.110).aspx