2013-11-27 49 views
0

我想通過區域ID seatch搜索進行搜索,並將其出現的描述,經理,事業部從數據庫而不在數據網格從搜索選擇。我想沒有數據網格

namespace Chemistlab.Addmanu 
{ 
    public partial class Zone : System.Web.UI.Page 
    { 
     string Sort_Direction = "ZoneID ASC"; 
     public int ZoneID 
     { 
      get { return (int)ViewState["ZoneID"]; } 
      set { ViewState["ZoneID"] = value; } 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       ZoneID = 0; 
       ViewState["SortExpr"] = Sort_Direction; 
       DataView dvStudents = Getdata(0); 
       GridVwPagingSorting.DataSource = dvStudents; 
       GridVwPagingSorting.DataBind(); 
       int n = GridVwPagingSorting.Rows.Count; 
      } 
     } 

     protected void Save_OnClick(object sender, EventArgs e) 
     { 
      string Description = textdes.Text.Trim(); 
      string Manager = textmanager.Text.Trim(); 
      string Division = textdivision.Text.Trim(); 
      string connectionString = ConfigurationManager.ConnectionStrings["ChemistlabConnectionString"].ConnectionString; 
      SqlCommand command = new SqlCommand(); 
      SqlConnection Connection = new SqlConnection(); 
      Connection.ConnectionString = connectionString; 
      Connection.Open(); 
      command.Connection = Connection; 
      command.CommandType = CommandType.Text; 
      if (btnSave.Text == "Save") 
      { 
       command.CommandText = "insert into tblzone values ('" + Description + "', '" + Manager + "','" + Division + "')"; 
      } 
      else 
      { 
       command.CommandText = "update tblzone set Description = '" + Description + "', Manager = '" + Manager + "', Division ='" + Division + "' where ZoneID =" + ZoneID; 
      } 
      int i = command.ExecuteNonQuery(); 
      Connection.Close(); 

      if (i > 0) 
      { 
       lblmsg.Text = "Save data Successfully."; 
       textzoneid.Text = ""; 
       textmanager.Text = ""; 
       textdivision.Text = ""; 
       textdes.Text = ""; 
       btnSave.Text = "Save"; 
      } 
      else 
      lblmsg.Text = "Unable to Save"; 
      // show data to gridview 

      ViewState["SortExpr"] = Sort_Direction; 
      DataView dvStudents = Getdata(0); 
      GridVwPagingSorting.DataSource = dvStudents; 
      GridVwPagingSorting.DataBind(); 
      int n = GridVwPagingSorting.Rows.Count; 

     } 
     private DataView Getdata(int zoneId) 
     { 
      using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ChemistlabConnectionString"].ToString())) 
      { 
       DataSet dsStudents = new DataSet(); 
       string strSelectCmd = ""; 
       if (zoneId > 0) 
       strSelectCmd = "SELECT [ZoneID],[Description],[Manager],[Division] FROM [tblzone] where ZoneID=" + zoneId; 
       else 
       strSelectCmd = "SELECT [ZoneID],[Description],[Manager], [Division] FROM [tblzone]"; 
       SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn); 
       da.Fill(dsStudents, "tblzone"); 
       DataView dvEmp = dsStudents.Tables["tblzone"].DefaultView; 
       dvEmp.Sort = ViewState["SortExpr"].ToString(); 
       return dvEmp; 


       //  foreach (GridView item in dvEmp.FindRows(ZoneID)) 
       //  { 

       //  } 
      } 
     } 
     protected void PageIndexChanging(object sender, GridViewPageEventArgs e) 
     { 
      GridVwPagingSorting.PageIndex = e.NewPageIndex; 
      DataView dvStudents = Getdata(0); 
      GridVwPagingSorting.DataSource = dvStudents; 
      GridVwPagingSorting.DataBind(); 
     } 

     protected void Sorting(object sender, GridViewSortEventArgs e) 
     { 
      string[] SortOrder = ViewState["SortExpr"].ToString().Split(' '); 
      if (SortOrder[0] == e.SortExpression) 
      { 
       if (SortOrder[1] == "ASC") 
       { 
        ViewState["SortExpr"] = e.SortExpression + " " + "DESC"; 
       } 
       else 
       { 
        ViewState["SortExpr"] = e.SortExpression + " " + "ASC"; 
       } 
      } 
      else 
      { 
       ViewState["SortExpr"] = e.SortExpression + " " + "ASC"; 
      } 
      GridVwPagingSorting.DataSource = Getdata(0); 
      GridVwPagingSorting.DataBind(); 
     } 

     ///protected void dataselect(object sender, GridViewSelectEventArgs e) 
     /// { 
     /// GridVwPagingSorting.ite 
     ///} 

     protected void dataselect(object sender, EventArgs e) 
     { 

      try 
      { 
       if (GridVwPagingSorting.SelectedIndex != -1) 
       { 
        GridViewRow row = GridVwPagingSorting.SelectedRow; 
        Label lblzoneId = (Label)row.FindControl("lblzoneId"); 
        Label lbldes = (Label)row.FindControl("lbldes"); 
        Label lblmanager = (Label)row.FindControl("lblmanager"); 
        Label lbldivsion = (Label)row.FindControl("lbldivsion"); 


        //-----------------------Selected item Display To Text or Dropdown List ------------------------------------ 
        ZoneID = Convert.ToInt32(lblzoneId.Text); 
        textzoneid.Text = lblzoneId.Text; 
        textdes.Text = lbldes.Text; 
        textmanager.Text = lblmanager.Text; 
        textdivision.Text = lbldivsion.Text; 
        btnSave.Text = "Update"; 
       } 
      } 
      catch (Exception ex) 
      { 

       lblmsg.Text = "Faild to Select data"; 
      } 
     } 

     protected void zoneid_TextChanged(object sender, EventArgs e) 
     { 
      ZoneID = Convert.ToInt32(textzoneid.Text); 
      //Getdata(Id); 

      DataView dvStudents = Getdata(ZoneID); 
      GridVwPagingSorting.DataSource = dvStudents; 
      GridVwPagingSorting.DataBind(); 
     } 


     //protected void zoneid_OnTextChanged(object sender, EventArgs e) 
     //{ 
     // ZoneID = Convert.ToInt32(textzoneid.Text); 
     // //Getdata(Id); 

     // DataView dvStudents = Getdata(ZoneID); 
     // GridVwPagingSorting.DataSource = dvStudents; 
     // GridVwPagingSorting.DataBind(); 
     //} 


    } 
} 
+2

所以你想通你會下降179行代碼我們,我們會爲你做....? – Noctis

+1

讓我們希望沒有管理員稱爲「'; DROP TABLE用戶 - 」@ User3041418一旦你得到這個工作,你真的需要使用描述,管理器,分區和ZoneId參數,而不是字符串連接。 –

+0

真正我在這個論壇是新的..請不要生氣....我只是不知道怎麼說和問.... – Hamid

回答

1

只需對數據庫執行SQL查詢,而不是循環遍歷網格中的項目。

查詢:

SELECT description, manager, division FROM [YOUR TABLE] WHERE ZONE_ID = [YOUR VALUE] 
+0

你會給我一個例子,請... – Hamid

+0

你甚至嘗試過編寫代碼來做到這一點? – JRO

相關問題