Dynamically data-bound columns versus asp:ButtonColumn

S

slapdash

I'm working on a small application where I'd like to have a
select column in a DataGrid displaying a result set returned from a
stored proc. Currently, I am doing something along these lines:

SqlConnection cn = new SqlConnection();
cn.ConnectionString = appSettingsConnectionString;
SqlCommand cmd = new SqlCommand(sqlCmd, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Connection.Open();
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[0];
DataGrid1.DataBind();

I then want to make one of the result columns not be visible,
so I tried:

DataGrid1.Columns[1].Visible = false;

However, that gives me an "index out of range" error. It
seems that the only column it knows about is the select button
column (an asp:ButtonColumn in the HTML tab of the web page in
the Visual Studio IDE). I had thought that the DataBind()
would add the other columns to the DataGrid1.Columns collection,
but apparently not; I suspect that without the select column
(requiring a Columns group in the HTML) it might work, but I
guess mixing the declared column (select) and dynamic columns
(from the DataSet) doesn't work?

I found this example (sorry for the long link; see Steven
Cheng's second response, the BindGrid method specifically):

http://groups.google.com/group/micr...pxGI_F6mjli6bgggzN3NiPdk9wNpVH3TDMOr3esSEFegR

Will I have to use this method, or is there some more automatic
method that I can use? It seems silly that a Columns collection
wouldn't actually have all the columns in it, so I'm hoping I'm
just missing something obvious because I'm still fairly new to
C#, ADO.NET and ASP.NET. Anyone have any suggestions?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top