How to hide row in datagrid

calvin

Regular
Joined
Nov 6, 2003
Messages
71
Hi,

Is anyone know how to hide the rows in a datagrid? The datagrid consists of a checkbox column, in certain situation, some row will be disable. When user click on a button, it will hide the row which checkbox has been disable

Thank you for any help.


Calvin
 
Hi Cavin,


You can use the DataView as datasource for your DataGrid. You can always set the filter on DataView to remove the rows.



HTH
 
Function BindTable()
sqlQuery = "Select Name, DOB, Gender from PersonalDetails"
cn.Open()
Dim da As New SqlDataAdapter(sqlGetCandidateDetails, cn)
dt = New DataTable("PersonalDetails")
da.Fill(dt)
DataGrid1.DataSource = dt
DataGrid1.DataBind()
cn.Close()

Dim i As Integer = 0
Dim item As DataGridItem
Dim cr As DataRow

For Each cr In dt.Rows
item = DataGrid1.Items(i)
Dim cb As CheckBox = CType(item.FindControl("cb"), CheckBox)

If cr.Item("Gender") = "M" Then
cb.Checked = True
cb.Enabled = False
End If
Next

End Function

Private Sub btnHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHide.Click
Dim i As Integer
Dim item As DataGridItem
Dim cb As CheckBox

For i = 0 To DataGrid1.Items.Count - 1
item = DataGrid1.Items(i)

cb = CType(item.FindControl("cb"), CheckBox)

If cb.Checked = True And cb.Enabled = False Then

'****************************
' How to DataView to hide row ??
Dim dv As DataView
?
?
?

End If
Next
End Sub


Calvin
 
Calvin,

You may store the data source in session object to make it available between posts to the server -

Function BindTable()
----------
----------
Session("Source") = dt
End Function


Private Sub btnHide_Click(.....)
----------
----------

'Get the indentifier of the records you want to exclude

If cb.Checked = True And cb.Enabled = False Then
'Get the values from the Grid and store in a variable
End If

'Considering you have populate a variable - NameList
' which contains the comma seperated names to be excluded

' Retrieve the data source from session state.
Dim dt As DataTable = CType(Session("Source"), DataTable)

' Create a DataView from the DataTable.
Dim dv As DataView = New DataView(dt)

'Now fileter the record in DataView and bind it to DataGrid
dv.RowFilter = "Name Not In '" & NameList & "%'"
DataGrid1.DataSource = dv
DataGrid1.DataBind()

End Sub

Do write to me for if any more clarification needed.

HTH
 
Private Sub btnHide_Click(.....)
----------
----------

'Get the indentifier of the records you want to exclude
** The identifier will be the checkbox(checked & disable)

If cb.Checked = True And cb.Enabled = False Then
'Get the values from the Grid and store in a variable
???Store all values to variable??
Dim strName as String = item.Cells(1).Text
Dim strDOB as String = item.Cells(2).Text
Dim strGender as String = item.Cells(3).Text
End If

'Considering you have populate a variable - NameList
' which contains the comma seperated names to be excluded
???what's the comma make???

' Retrieve the data source from session state.
Dim dt As DataTable = CType(Session("Source"), DataTable)

' Create a DataView from the DataTable.
Dim dv As DataView = New DataView(dt)

'Now fileter the record in DataView and bind it to DataGrid
dv.RowFilter = "Name Not In '" & NameList & "%'" <-???(filter the names that start with text in variable "NameList???

dv.RowFilter = strName
dv.RowFilter = strDOB
dv.RowFilter = strGender

DataGrid1.DataSource = dv
DataGrid1.DataBind()

End Sub

Calvin
 
Hi,

** The identifier will be the checkbox(checked & disable)

Nope, by identifier i mean the column which identifies your record uniquely in your database.

You need a correponding value inorder to identify which rows to be excluded.

I would suggest you to include the one more field (may be PK of the table) in your select statement.

Then you can have a invisible column in datagrid to store your unique record identifier.

Now,

'Declare an Array to hold IDs
dim UniqueIDList() as string

For i = 0 To DataGrid1.Items.Count - 1
item = DataGrid1.Items(i)
cb = CType(item.FindControl("cb"), CheckBox)

If cb.Checked = True And cb.Enabled = False Then
'Store the value of IDs to array
'Considering the Cells(0) has the IDs

'UniqueIDList(0) = item.Cells(0).Text

End If
Next

Then, Join the array and make a comma seperated string of IDs.
'Dim sID as string
'sID= UniqueIDList.Join(",")

Now, as you have the ID list, use it in DataView.filter.

Please check the syntax of the above code and modify, if needed. I need to switch too often in between VB.NET and C# for my work. So, most of the time i end up mixing the syntaxes of both:)


HTH
 
I get an error on sID= UniqueIDList.Join(",")

Error Message: 'Join' is not a member of 'System.Array'

Dim UniqueIDList() as String
For....
'UniqueIDList(0) = item.Cells(0).Text
Next

-------------------------------------------------------------------------
Is UniqueIDList declare correctly on the above?(declare as String?) How to do the looping for UniqueIDList.

Dim j as integer = 0
For..
UniqueIDList(j) = item.Cells(0).Text
j += 1
Next


Calvin
 
error again:
Object reference not set to an instance of an object.

UniqueIDList(0) = item.Cells(0).Text
 
Hi,


'Declare an Array to hold IDs
dim UniqueIDList() as string

For i = 0 To DataGrid1.Items.Count - 1
item = DataGrid1.Items(i)
cb = CType(item.FindControl("cb"), CheckBox)

If cb.Checked = True And cb.Enabled = False Then

'You need to resize the array here
ReDim Preserve UniqueIDList(i)


'Store the value of IDs to array
'Considering the Cells(0) has the IDs

UniqueIDList(i) = item.Cells(0).Text

End If
Next



Notice , the Redim statement above.

I would strongly suggest you to go through some tutorial on Array usage in VB.NET.

HTH
 
Syntax error: Missing operand before ',' operator.

dv.RowFilter = sID

I think this is made error -> sID = String.Join(",", UniqueIDList)
 
Hi,

You can't just write
dv.RowFilter = sID

It should be like:

'Now fileter the record in DataView and bind it to DataGrid
dv.RowFilter = "YourIDField Not In (" & sID & ")"


You can have any delimiter as long as they don't mess up with your data item.


HTH
 
I got this error:

Syntax error: Missing operand before ',' operator.

So, I check the sID -> Response.Write(sID)
It was a comma prefix the uniqueID. For Example: ,UniqueID,Unique

Then I try to remove the comma in join funtion like this
sID = String.Join("", UniqueIDList)

And get this error:

Cannot find column [YourIDField].
 
Oh, I put wrong IDFieldName in the "YourIDField". But I still get same error of Syntax error: Missing operand before ',' operator.

As I mentioned above, I try to show the string of sID, it show like below:
Output:

,abc0001,abc0003,abc0004


I believe that the comma in the begining cause the error. How can I remove that?

I had try to used one of the UniqueID like below and successfully get the result.
dv.RowFilter = "UniqueID <> 'abc0001'"

So, how can I solve this problem?

Calvin
 
Calvin,

Instead of thinking about removing the "," from the begining of the string you should find out the reason as to why it is there.

The obvious reason could be that the first value of item.Cells(0).Text in blank.

Now, you may would like to check item.Cells(0).Text value before it is stored in the array.

Something like :

For....
if item.Cells(0).Text <> "" then
UniqueIDList(i) = item.Cells(0).Text
end if
Next

But then you should also investigate that why is it blank.

I would again suggest you go through some good book. And through tutorial and try looking at the problem logically.

HTH
 
No value in item.Cells(0).Text is blank, I did check for it.

Like this
For....
UniqueIDList(i) = item.Cells(0).Text
Response.Write(UniqueIDList(i))
Next

But If I show the sID, it must show the comma from the begining
 
You are right, first value of item.Cells(0).Text in blank. But I store the value to the UniqueIDList(i) is when the checkbox was checked and also being disable.
 
Back
Top