'Gridview.Databind() not refresh the data
First of all, sorry if I make mistakes in english...
I'm making a web with c#, and I have some problems for refresh the data displayed in the GridView
I'm getting the data throw the SqlDataSource defined at the aspx view:
<asp:SqlDataSource ID="PRODUCTOS_CON_STOCK" runat="server" ConnectionString="<%$ ConnectionStrings:XXXX %>" ProviderName="<%$ ConnectionStrings:XXX.ProviderName %>" DataSourceMode="DataSet" SelectCommand=" select EAN, CODART..... "> </asp:SqlDataSource>
When I click a button, I update some data in the database, and I want refresh de GridView with the new data, without reload the page.
I'm making: gridView.DataBind();, but that doesn't refresh the data in the GridView.
(At the database is updated)
The GridView is inside of an UpdatePanel.
I was trying some things, like:
Reassing the
DataSourceIDand make thegridView.DataBind();Assing the
DataSourceIDinnull, make thegridView.DataBind();, and alter reassing theDataSourceIDand make thegridView.DataBind();I tried too:
DataSourceSelectArguments argumentos = new DataSourceSelectArguments();
PRODUCTOS_CON_STOCK.Select(argumentos);
gridView.DataBind();
- Set the
UpdatePaneltoupdatemode="Always"
But any of all of that worked... Someone can help me?
Thanks.
Solution 1:[1]
Why not dump the datasource setting on the page of PRODUCTOS_CON_STOCK.
I find that when you need to filtere, load, and play?
Just remove the data source from the markup. So, in your GridView, remove the data source id setting of PRODUCTOS_CON_STOCK, and then delete the data source.
You have to write a BIT more code, but you now have control over the data, and MORE important, control over which parameters you need and want.
So, say I have this markup:
A grid, and also a search/text box to filter the grid.
<asp:Label ID="Label1" runat="server" Text="Search for Fighter jet" Font-Size="Large"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" Style="margin-left:15px" Font-Size="Large">
</asp:TextBox>
<asp:Button ID="cmdSearch" runat="server" Text="search"
style="margin-left:15px" CssClass="btn" OnClick="cmdSearch_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" >
<Columns>
<asp:BoundField DataField="Fighter" HeaderText="Fighter" />
<asp:BoundField DataField="Engine" HeaderText="Engine" />
<asp:BoundField DataField="Thrust" HeaderText="Thrust" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:ImageButton ID="btnImage" runat="server" Height="68px" Width="149px"
OnClientClick ="popimage(this);return false"
ImageUrl = '<%# Eval("ImagePath") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Note close - I did build this grid using the wizards. But I THEN removed the Datasource ID for the GV, and removed the Datasource that was created in the markup.
So, now my code to load is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid("");
}
void LoadGrid(string MySearch)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from Fighters ", conn))
{
if (MySearch != "")
{
cmdSQL.CommandText += @" WHERE Fighter LIKE @Fighter + '%'";
cmdSQL.Parameters.Add("Fighter", SqlDbType.NVarChar).Value = MySearch;
}
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
GridView1.DataSource = rstData;
GridView1.DataBind();
}
}
}
And I get this:
And note the "optional" filter for the text box. If you type in some text, (I used a "like" match in sql), then the code is only this:
protected void cmdSearch_Click(object sender, EventArgs e)
{
LoadGrid(txtSearch.Text);
}
but, the idea here is often it is much better to dump the SqlDataSoruce placed in the markup, and roll + write your own code. The problem is you can try and set the data source in code, but ALSO WITH a data source on the page - they fight over each other. So, try the above idea - and remove the data source in the markup.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Albert D. Kallal |

