2015-12-29 32 views
0

我面临一个问题不知道如何设置这个问题问题是,我的项目工作正常,但是当我从SQL数据库删除所有行它不显示网格好心帮 你的反应将高度赞赏GridView不显示当我删除数据库表中的所有行

这里是我的代码背后

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)); 
         } 
        } 
       } 
      } 
     } 
+0

”不是个如果你在数据库中删除它们,这是否正常?我在代码中看不到_any_ delete命令。 –

+1

为什么它会显示gridview,如果你已经删除了所有的记录?如果您尝试在没有行时设置任何默认文本,则可以使用[EmptyDataText属性](https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.emptydatatext (v = vs.110).aspx)或[EmptyDataTemplate Property](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate(v = vs.110)的.aspx)。 –

+0

@RahulSingh用于在网格视图中插入我正在使用网格 – Malick

回答

0

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

相关问题