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?