Programatically accessing and manipulating GridView for search criteria with Stored Procedure

Last post 04-02-2008 2:24 AM by Qin Dian Tang - MSFT. 2 replies.

Sort Posts:

  • Programatically accessing and manipulating GridView for search criteria with Stored Procedure

    03-30-2008, 10:06 AM
    • Loading...
    • Siby2000
    • Joined on 03-03-2008, 12:07 AM
    • Posts 72

    Can anybody help with some example to programatically access the GridView.  Also we should be able to manipulate the gridview data  for different search criteria, with a drop down fields, text box search box, checkbox etc.  All this should be done through stored procedures and no HTML code, no inline code, Only code behined model.  Could anyone provide a sample code in VB or C#.

    Thanks

     

  • Re: Programatically accessing and manipulating GridView for search criteria with Stored Procedure

    03-30-2008, 11:05 AM
    • Loading...
    • crfenix
    • Joined on 06-19-2007, 1:19 PM
    • Buenos Aires, Argentina
    • Posts 154

    Hi!

    You can use the objectDataSource or the sqldatasource as dataprovider.

    Using ObjectDataSource (Bussines object as data provider)

    http://www.codeproject.com/KB/aspnet/GridviewObjectDataSrc.aspx

    Using SqlDataSource (Stored Procedure as data provider)

    http://msdn2.microsoft.com/en-us/library/k10148y1(VS.80).aspx

     For both approach is important to pay attention to select paramenters, you will need to use Control Parameters in order to implement the search funcionality.

    Regards

    Simplicity: "the art of maximizing the amount of work not done."
  • Re: Programatically accessing and manipulating GridView for search criteria with Stored Procedure

    04-02-2008, 2:24 AM
    Answer

    Hi Siby2000,

    Here is a sample for filtering data from DropDownList control:

    string query = "select * from table";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = GetData(query);
                GridView1.DataBind();
            }
        }

        public DataSet GetData(string query)
        {
            SqlConnection conn = new SqlConnection("Connection String");
            SqlCommand cmd = new SqlCommand(query, conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            return ds;
        }

        protected void Filter_Click(object sender, EventArgs e)
        {
            string id = SearchTextBox.Text;
            if (id != "")
                query = "select * from table where ID=" + id;
            GridView1.DataSource = GetData(query);
            GridView1.DataBind();
        }

    Other controls are similar to this, so you can try them by yourself. Remember to pass search text into query and then retrieve data to bind GridView.

    Thanks,

    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter