我想搜索而不在datagrid中搜索

时间:2013-11-27 12:16:52

标签: c# asp.net database search datasource

我想按区域ID进行搜索,并且它会从数据库中显示描述,管理员,分区,而不从数据网格中进行选择。

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();
        //}


    }
} 

1 个答案:

答案 0 :(得分:1)

只需对数据库执行SQL查询,而不是循环遍历网格中的项目。

查询:

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