Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
slapdash #1
Dynamically data-bound columns versus asp:ButtonColumn
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):
[url]http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.datagridc ontrol/browse_thread/thread/32c8b072328a7539/d2e224f9e4c20e0a?lnk=st&q=%22asp.net%22+datagrid+b uttoncolumn+visible&rnum=3&hl=en&auth=DQAAAGYAAACl JkjPZHG3afnCgcfJQg_AAPtvW16DptvNe0nsfRHZV4Z_8bnW5p XVwtOQKrGFTa0paSgQmV_dr5OKcqkCmukkQKB8GUmPp76yFkiW 3xHpxGI_F6mjli6bgggzN3NiPdk9wNpVH3TDMOr3esSEFegR[/url]
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?
slapdash Guest
-
bound columns of a datagrid
i have a datagrid with few bound columns.i want to make the first column clickable.can i make a bound column clickable and write a handler for... -
DG-Edit-Loosing data; bound columns - pushbuttons; dataReader
Dear Readers, I am using C# (vb examples will work for me too - i am vb progrmr). I created a dg (datagrid) with bound columns (hesistant to... -
Dynamically Adding Bound Columns Won't Sort
All- I'm basically creating a user control which contains a datagrid that will be used throughout our application by all pages. My user control... -
iterate through bound columns
Hello, I have several pages with datagrids, and I was thinking of writing a common excel function. I want to iterate through the columns of... -
can't get data to show with bound columns
I'm trying to create a DataGrid filled with values from a DataSet and I want the user to be able to show and hide whichever columns they choose. I...



Reply With Quote

