3

HTML

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
   <Columns>
       <asp:BoundField DataField="id" HeaderText="id"  />
       <asp:BoundField DataField="name" HeaderText="name" />
   </Columns>
 </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:database1ConnectionString %>"
SelectCommand="SELECT * from tblCourse"></asp:SqlDataSource>

Code

 SqlDataSource1.SelectCommand =
        "SELECT  * from tblCourse where name='"+textbox1.text+"'";
  SqlDataSource1.DataBind();

But Gridview does not change based on the new select command, even when I'm using DataBind()

How can we change grid view base on select command of sql data source?

Jaison Varghese
  • 1,302
  • 1
  • 13
  • 24
user1263390
  • 187
  • 3
  • 4
  • 12

5 Answers5

3

This is happening because of GridView's viewstate.

When postback happens, gridview stores its data from ViewState. So, You can either turn off view state for GridView ( a good practice ?? ) OR you call GridView.DataBind() in addition to SqlDataSource.Databind();

METHOD 1: Calling GridView.DataBind();

protected void Page_Load(object sender, EventArgs e)
 {
     if (this.IsPostBack)
     {
        string command = SqlDataSource1.SelectCommand; // added just for debug purpose
        SqlDataSource1.SelectCommand = "SELECT  * from tblCourse where 
                                        name='"+textbox1.text+"'";
        SqlDataSource1.DataBind();
        gridview1.DataBind();
      }

  }

METHOD 2: Turn off View State for GridView ( Is this a good Practice? ). When you set this false, there is NO need to call GridView.DataBind() in your page_Load as seen in above METHOD 1.

<asp:GridView runat="server" ID="gridview1" EnableViewState="false" ...  />

Now the part comes that should must be taken care of::

Make sure the <asp:BoundField> or in general any fields declared and bound to GridView markup are also present in your new query else an error will be thrown saying something similar as below:

A field or property with the name 'ID' was not found on the selected data source
R.C
  • 10,417
  • 2
  • 35
  • 48
1
string strSql= "SELECT  * from tblCourse where name='abc'";
ViewState["SQL"]=strSql;
SqlDataSource1.SelectCommand =strSql;        
SqlDataSource1.DataBind();

Now in the Page_Load

if(IsPostback)
     SqlDataSource1.SelectCommand=ViewState["SQL"].ToString();
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
0

Set autogenerate to true and be sure to remove all the fields in the edit fields wizard. If you change the select command it causes the conflict.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jacque
  • 1
0

Try adding the following :

 SqlDataSource.select(DataSourceSelectArguments.Empty);

before the line SqlDataSource.DataBind();

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
geo
  • 1
0

You can either edit your BoundField's or change the property AutoGenerateColumns to true.

shriek
  • 5,157
  • 2
  • 36
  • 42