calvin Posted October 4, 2004 Posted October 4, 2004 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 Quote
Jitesh Posted October 4, 2004 Posted October 4, 2004 Hi, Simply remove the rows from the datasource and bind it to datagrid on post back. HTH Quote Jitesh Sinha
Jitesh Posted October 5, 2004 Posted October 5, 2004 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 Quote Jitesh Sinha
calvin Posted October 5, 2004 Author Posted October 5, 2004 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 Quote
Jitesh Posted October 5, 2004 Posted October 5, 2004 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 Quote Jitesh Sinha
calvin Posted October 5, 2004 Author Posted October 5, 2004 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 Quote
Jitesh Posted October 5, 2004 Posted October 5, 2004 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 Quote Jitesh Sinha
calvin Posted October 5, 2004 Author Posted October 5, 2004 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 Quote
Jitesh Posted October 5, 2004 Posted October 5, 2004 **I get an error on sID= UniqueIDList.Join(",") My mistake. Try this: sID= String.Join(",",UniqueIDList) For more see this, http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=169&lngWId=10 HTH Quote Jitesh Sinha
calvin Posted October 5, 2004 Author Posted October 5, 2004 error again: Object reference not set to an instance of an object. UniqueIDList(0) = item.Cells(0).Text Quote
Jitesh Posted October 5, 2004 Posted October 5, 2004 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 Quote Jitesh Sinha
calvin Posted October 5, 2004 Author Posted October 5, 2004 Syntax error: Missing operand before ',' operator. dv.RowFilter = sID I think this is made error -> sID = String.Join(",", UniqueIDList) Quote
calvin Posted October 5, 2004 Author Posted October 5, 2004 Can we use other as delimeter instead of comma? Quote
Jitesh Posted October 5, 2004 Posted October 5, 2004 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 Quote Jitesh Sinha
calvin Posted October 5, 2004 Author Posted October 5, 2004 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]. Quote
calvin Posted October 6, 2004 Author Posted October 6, 2004 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 Quote
Jitesh Posted October 6, 2004 Posted October 6, 2004 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 Quote Jitesh Sinha
calvin Posted October 6, 2004 Author Posted October 6, 2004 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 Quote
calvin Posted October 6, 2004 Author Posted October 6, 2004 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. Quote
calvin Posted October 6, 2004 Author Posted October 6, 2004 Oh, I know what's wrong. We use same (i) as integer with For loops. So replace "i" to "j".However still have some error Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.