SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable gridview - how do I do this?

    I have some web page that has a number of different GridView objects. So for each GridView, I run the following code to bind specific data to that gridview.

    For code below binds the to 20100813 grid - and I have similar code for 20100713, 20100613, etc.

    protected void Exp20100813(object sender, EventArgs e)
    {

    //Define data objects
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    //Read the connection string from webconfig
    string connectionString = ConfigurationManager.ConnectionStrings["HostServer"].ConnectionString;
    //initialize connection
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand(sqlQuery, conn);
    try
    {
    conn.Open();
    reader = comm.ExecuteReader();
    rGrid20100813.DataSource = reader;
    rGrid20100813.DataKeyNames = new string[] { "IDNUMBER" };
    rGrid20100813.DataBind();
    reader.Close();
    }
    I would like to just have one set of code, where I set the ID of the gridview and then bind to it that way.

    I tried to set the ID this way, but this didnt work.
    GridView rGridViewVar = new GridView();
    rGridViewVar.ID = "rGrid" + "20100813";
    ...
    try
    {
    conn.Open();
    reader = comm.ExecuteReader();
    rGridViewVar.DataSource = reader;
    So how do I use a variable this way?

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help? Basically just need to know how to access a control with a variable.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Probably the best bet is to make your method take a GridView as an argument rather than trying to instantiate it directly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •