GridView Data Binding at runtime

Discussion in 'ASP .Net Web Controls' started by MoonWa, Jan 20, 2006.

  1. MoonWa

    MoonWa Guest

    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();
    MoonWa, Jan 20, 2006
    #1
    1. Advertising

  2. 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.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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();
    >
    Phillip Williams, Jan 20, 2006
    #2
    1. Advertising

  3. MoonWa

    MoonWa Guest

    Thanks Phillip

    Jonathan
    MoonWa, Jan 20, 2006
    #3
  4. In your button click event, try:

    eventEditGV.DataBind();

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

    "MoonWa" <> wrote in message
    news:D...
    >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();
    >
    Christopher Reed, Jan 22, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan
    Replies:
    2
    Views:
    2,530
    Jordan
    Feb 10, 2004
  2. Amit
    Replies:
    6
    Views:
    13,791
    Assimalyst
    Oct 24, 2006
  3. =?Utf-8?B?UGhpbGlw?=

    GridView - ObjectDataSource - Data Binding

    =?Utf-8?B?UGhpbGlw?=, Oct 24, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,007
    =?Utf-8?B?UGhpbGlw?=
    Oct 24, 2006
  4. Saint

    Gridview data binding HELP!!

    Saint, Dec 24, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    313
    Bill Nicholson
    Dec 24, 2006
  5. JcFx
    Replies:
    0
    Views:
    276
Loading...

Share This Page