Dynamic DataGrid

J

Joe Goeke

I am attempting to use a DataGrid to dynamically display data based on what
category the user picks. My data is in a SQL Server DB. My current problem
is that I can't seem to find the best way to display the data in the
DataGrid.

For example, when the user picks a category (wing nuts), I will have data in
the Description field, description1 field, and the Dimension1 field. All
other fields in the DB will be null. If the user picks screws, then I will
have data in the Description, Description1, description2, description3,
Dimension1, dimension2, and dimension 3 fields, with all other fields being
null.

My quandry is that I can't figure out the best way to do this. Should I
write a SQL statement that does not return the null fields or should I try
to not display the null fields when populating the DataGrid. I have been
currently working on this for a week and have not had much luck in finding
the answer.

I have done this same thing in Classic ASP, but am having trouble with an
ASP.NET solution.

Thanks in advance for any assistance.

Joe
 
S

Sachin Saki

Hi,

Either both the way you can do. You can write stored procedure that can
return Only columns that have values OR you can set visible property of
particular column in datagrid which having value NULL in Itemdatabound Event.

I suggest you to go with second option because it will be genric only you
have to set columns visiblity instead of modifing storedprocedure everytime
because you donot which column having data or not.

I hope this will work for you, any suggestion will be welcome

Regards,
Sachin Saki
..NET Developer - Capgemini, INDIA

"Joe Goeke" ने लिखा:
 
J

Joe Goeke

Sachin,

Thanks for the reply, but I'm still not understanding how this thing is
supposed to work. Here is my code as I have it so far.

Dim strFamily as String
strFamily = Request.QueryString("fam")
Dim strConnection as String, sqlConn as SQLConnection
strFamily = Request.QueryString("fam")
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)

Dim MyDataAdapter as SQLDataAdapter, MyDataSet as New DataSet

MyDataAdapter = New SQLDataAdapter("SELECT * FROM Items WHERE Part = '"
& Request.QueryString("part") & "'", strConnection)

MyDataAdapter.Fill(MyDataSet, "Items")
Dim i as Integer

'To navigate through the records.
For i = 0 To MyDataSet.Tables(0).Columns.Count - 1
Dim objbc As New BoundColumn()
objbc.DataField = MyDataSet.Tables(0).Columns(i).ColumnName
objbc.HeaderText = MyDataSet.Tables(0).Columns(i).ColumnName

'If I take out this statement, all columns (including nulls) display in
the DataGrid. With this statement, the DataGrid does not display at
all.
If MyDataSet.Tables(0).Rows(0).Item("Description3") Is
System.DBNull.Value Then
objbc.Visible = False
End If

dgItemDetail.Columns.Add(objbc)
dgItemDetail.DataSource = MyDataSet.Tables(0)
dgItemDetail.DataBind()
Next 'i

Thx,

Joe
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top