|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
GridView Data Binding at runtimeI have added a GRidView to my page but have not bound the data yet. How do I
bind my GridView to a SqlDataSource when a user clicks a button. Here is the code I have but it does not work. SqlDataSource editDS = new SqlDataSource(); editDS.ID = "editDS"; editDS.ConnectionString = WebConfigurationManager.ConnectionStrings["collaborativesqlSecureConnectionString"].ConnectionString; editDS.ProviderName = "System.Data.SqlClient"; editDS.SelectCommand = "SELECT [EventId], [EventDateTime] FROM [MainEvents] WHERE " + dateStr + " LIKE EventDateTime"; // Bind GridView to data source eventEditGV.DataSourceID = editDS.ID; Page.DataBind(); You missed to add the SqlDataSource to your page's controls collection, by
executing the following line before you assigned its ID to the DAtaSourceID of the GridView: Page.Controls.Add(editDS.); SqlDataSource is a server control that has to be added to the Page's Controls collection before you can use it. You could have alternatively carried out the entire DataSet retrieval programmatically like this: private System.Data.DataSet GetMainEventsDS(string dateStr ) { string connectionString = ConfigurationManager.ConnectionStrings["collaborativesqlSecureConnectionString"].ConnectionString; System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString); string queryString = "SELECT [EventId], [EventDateTime] FROM [MainEvents] WHERE " + dateStr + " LIKE EventDateTime"; System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand(); dbCommand.CommandText = queryString; dbCommand.Connection = dbConnection; System.Data.IDbDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(); dataAdapter.SelectCommand = dbCommand; System.Data.DataSet dataSet = new System.Data.DataSet(); dataAdapter.Fill(dataSet); return dataSet; } And then assign the dataset to the GridView's DataSource property or through an ObjectDataSource. Show quoteHide quote "MoonWa" wrote: > I have added a GRidView to my page but have not bound the data yet. How do I > bind my GridView to a SqlDataSource when a user clicks a button. Here is the > code I have but it does not work. > > SqlDataSource editDS = new SqlDataSource(); > editDS.ID = "editDS"; > editDS.ConnectionString = > WebConfigurationManager.ConnectionStrings["collaborativesqlSecureConnectionString"].ConnectionString; > editDS.ProviderName = "System.Data.SqlClient"; > editDS.SelectCommand = "SELECT [EventId], [EventDateTime] FROM > [MainEvents] WHERE " + dateStr + " LIKE EventDateTime"; > > // Bind GridView to data source > eventEditGV.DataSourceID = editDS.ID; > > Page.DataBind(); > In your button click event, try:
eventEditGV.DataBind(); -- Show quoteHide quoteChristopher A. Reed "The oxen are slow, but the earth is patient." "MoonWa" <Moo***@discussions.microsoft.com> wrote in message news:D9BCD7D8-90A4-4D26-8D2C-E9451B0C5FFB@microsoft.com... >I have added a GRidView to my page but have not bound the data yet. How do >I > bind my GridView to a SqlDataSource when a user clicks a button. Here is > the > code I have but it does not work. > > SqlDataSource editDS = new SqlDataSource(); > editDS.ID = "editDS"; > editDS.ConnectionString = > WebConfigurationManager.ConnectionStrings["collaborativesqlSecureConnectionString"].ConnectionString; > editDS.ProviderName = "System.Data.SqlClient"; > editDS.SelectCommand = "SELECT [EventId], [EventDateTime] FROM > [MainEvents] WHERE " + dateStr + " LIKE EventDateTime"; > > // Bind GridView to data source > eventEditGV.DataSourceID = editDS.ID; > > Page.DataBind(); >
ITemplate - Dynamic ImageButton in BindLabelColumn for DataList
Getting value from child control of formview Embedding CSS with inline code "Protect" or Disable Editing of A Webcontrol SmartNavigation Problem/Bug Reports and the ReportViewer Dataset from ObjectDataSource always null? GUID and data controls DotNet & SDK install issue Updating component attributes |
|||||||||||||||||||||||