Home All Groups Group Topic Archive Search About

GridView Data Binding at runtime

Author
20 Jan 2006 1:40 PM
MoonWa
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();

Author
20 Jan 2006 4:08 PM
Phillip Williams
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();
>
Author
20 Jan 2006 7:01 PM
MoonWa
Thanks Phillip

Jonathan
Author
22 Jan 2006 2:26 PM
Christopher Reed
In your button click event, try:

eventEditGV.DataBind();

--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

Show quoteHide quote
"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();
>