Click here to Skip to main content
15,898,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagrid which is already binded to a DB.
how to display data on the datagrid when a button is clicked ?
Posted

Hi,
In the button click event

SqlDataAdapter adp = null;
DataSet ds = null;
try
{
adp = new SqlDataAdapter("your query", connection string);
adp.SelectCommand.CommandTimeout = 100000;
ds = new DataSet();
adp.Fill(ds);
DataGridview.DataSource=ds;
//For Web application add one more line
DataGridview.DataBind();
}
catch (SqlException ex)
{
throw;
}
 
Share this answer
 
Comments
BuBa1947 13-Dec-12 5:24am    
is it this way ?

dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
U can bind ur DB TO datagrid, into the button click event.
 
private void btn_Click(object sender, EventArgs e)
{
String Query=Select * from UrTableName"
SqlDataAdapter DA=new SqlDataAdapter(Query,UrConnectionStringName);
DataSet DS=new DataSet();
DA.fill(DS);
URDATAGRIDVIEWNAME.datasource=ds;
}
 
Share this answer
 
v5
Comments
BuBa1947 13-Dec-12 4:58am    
Thanks!
StackQ 13-Dec-12 5:02am    
like at the button click event-
urDataGridName.datasource= UrDataSourceName

r u statified?
BuBa1947 13-Dec-12 5:24am    
is it this way ?

dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
StackQ 13-Dec-12 5:25am    
ya

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900