Current location: Hot Scripts Forums » Programming Languages » ASP.NET » [SOLVED] searching through a grid view


[SOLVED] searching through a grid view

Reply
  #1 (permalink)  
Old 05-20-08, 03:36 AM
painthu painthu is offline
New Member
 
Join Date: May 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] searching through a grid view

hi,

i am creating a custom control- a grid view..
i have been asked to create search options such that under each column's header in the grid view there will be a textbox and then when the user types int something in any of the textboxes and hits search button ,he can actually see the grid getting populated with the relevant rows.

i was successfull in doing this in a normal program..ie aspx in code behind, but u see the problem is that in the case of a custom control, the grid view can be populated using any table from sql server, i don't know what common logic can be put under search section in a custom grid view....


i mean i was successful in creating textboxes under each column in the custom control-grid view...but it's the search function that i am uanble to incorporate....any ideas....?


the custom control code:-
csharp Code:
  1. the code is as follows:-
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Data.SqlClient;
  10. using System.Data;
  11. namespace customcon
  12. {
  13.  
  14.  
  15. public class WebCustomControl6 : System.Web.UI.WebControls.GridView
  16. {
  17. SqlConnection con = new SqlConnection();
  18. string s;
  19. DataSet ds = new DataSet();
  20. SqlDataAdapter da = new SqlDataAdapter();
  21. SqlCommandBuilder scb = new SqlCommandBuilder();
  22. public GridView grid;
  23. public DropDownList txtSearch;
  24. public TextBox txtSearch1;
  25. public TextBox txtSearch2;
  26. public TextBox txtSearch3;
  27. public TextBox txtSearch4;
  28. public TextBox txtSearch5;
  29. public TextBox txtSearch6;
  30. public string f;
  31.  
  32. protected override void OnRowCreated(GridViewRowEventArgs e)
  33. {
  34. base.OnRowCreated(e);
  35. if (e.Row.RowType == DataControlRowType.Header)
  36. {
  37. Literal litHeader;
  38. TextBox txtSearch;
  39.  
  40.  
  41. //GridViewRow se=new GridViewRow(0,1,DataControlRowType.Header,
  42.  
  43. for(int i = 0; i <= (e.Row.Cells.Count - 1); i++)
  44. {
  45. for (int z = 0; z <= (e.Row.Cells.Count - 1); z++)
  46. {
  47. if (i == z)
  48. {
  49. litHeader = new Literal();
  50. txtSearch = new TextBox();
  51. txtSearch.ID = "txtname" + z;
  52. txtSearch.Width = 35;
  53. litHeader.Text = e.Row.Cells&#91;z].Text + "<br />";
  54. e.Row.Cells&#91;z].Controls.Add(litHeader);
  55. e.Row.Cells&#91;z].Controls.Add(txtSearch);
  56.  
  57. }
  58. }
  59. }
  60.  
  61. }
  62. }
  63. protected override void OnDataBound(EventArgs e)
  64. {
  65. base.OnDataBound(e);
  66.  
  67. this.CreateSecondHeader();
  68.  
  69. }
  70. private SqlConnection connection
  71. {
  72. get
  73. {
  74. return (SqlConnection)this.ViewState&#91;"connection"];
  75.  
  76. }
  77. set
  78. {
  79. this.ViewState&#91;"connection"] = value;
  80. }
  81.  
  82. }
  83. private void btnclick(DataTable dt,SqlConnection connection)
  84. {
  85. string s = "select * from " + dt + "where" + this.Columns&#91;0].HeaderText + "like" + "'" + txtSearch.Text + "'";
  86. da = new SqlDataAdapter(s, connection);
  87. scb = new SqlCommandBuilder(da);
  88. ds = new DataSet();
  89. da.Fill(ds);
  90. ds.Tables.Add(dt);
  91. this.DataSource = dt;
  92. this.DataBind();
  93.  
  94. }
  95.  
  96.  
  97.  
  98. private void CreateSecondHeader()
  99. {
  100. GridViewRow row = new GridViewRow(0,1, DataControlRowType.DataRow, DataControlRowState.Normal);
  101.  
  102. Button btn = new Button();
  103. btn.Width = Unit.Pixel(60);
  104. btn.Text = "search";
  105. Button btn1 = new Button();
  106. btn1.Width = Unit.Pixel(60);
  107. btn1.Text = "cancel";
  108.  
  109. TableCell th = new TableCell();
  110. th.Controls.Add(btn);
  111.  
  112. th.HorizontalAlign = HorizontalAlign.Center;
  113. th.ColumnSpan = this.Columns.Count;
  114. TableCell th1 = new TableCell();
  115. th1.Controls.Add(btn1);
  116. th.HorizontalAlign = HorizontalAlign.Center;
  117. th.ColumnSpan = this.Columns.Count;
  118. //th.Width =this.Width;
  119.  
  120. row.Cells.Add(th);
  121. row.Cells.Add(th1);
  122.  
  123. this.InnerTable1.Rows.AddAt(0, row);
  124. }
  125. protected Table InnerTable1
  126. {
  127. get
  128. {
  129. if (this.HasControls())
  130. {
  131. return (Table)this.Controls&#91;0];
  132. }
  133.  
  134. return null;
  135. }
  136. }
  137.  
  138.  
  139.  
  140. }
  141. }

the code for a normal aspx code behind:-:-

vbnet Code:
  1. Imports System
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Partial Class Default2
  5.     Inherits System.Web.UI.Page
  6.     Dim con As New SqlConnection
  7.     Dim s As String
  8.     Dim ds As New DataSet
  9.     Dim da As New SqlDataAdapter
  10.     Dim scb As New SqlCommandBuilder
  11.     Dim dt As New DataTable
  12.     Public WithEvents txtSearch As DropDownList
  13.     Public WithEvents txtSearch1 As TextBox
  14.     Public WithEvents txtSearch2 As TextBox
  15.     Public WithEvents txtSearch3 As TextBox
  16.     Public WithEvents txtSearch4 As TextBox
  17.     Public WithEvents txtSearch5 As TextBox
  18.     Public WithEvents txtSearch6 As TextBox
  19.     Public f As String
  20.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  21.         con = New SqlConnection("server=DCPLDC;user id=sa;password=crorepathy;database=Testing")
  22.         con.Open()
  23.         s = "SELECT Id,Label,Textbox FROM textboxtable"
  24.         da = New SqlDataAdapter(s, con)
  25.         ds = New DataSet
  26.         scb = New SqlCommandBuilder(da)
  27.         da.Fill(ds)
  28.         ds.Tables.Add(dt)
  29.         dt = New DataTable("textboxtable")
  30.         BulkEditGridView1_2.DataSource = ds.Tables(0)
  31.         BulkEditGridView1_2.DataBind()
  32.         con.Close()
  33.     End Sub
  34.     Sub binddata()
  35.         s = "SELECT Id,Label,Textbox FROM textboxtable"
  36.         da = New SqlDataAdapter(s, con)
  37.         ds = New DataSet
  38.         scb = New SqlCommandBuilder(da)
  39.         da.Fill(ds)
  40.         ds.Tables.Add(dt)
  41.         dt = New DataTable("textboxtable")
  42.         BulkEditGridView1_1.DataSource = ds.Tables(0)
  43.         BulkEditGridView1_1.DataBind()
  44.     End Sub
  45.     Protected Sub BulkEditGridView1_1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles BulkEditGridView1_1.RowCreated
  46.         If e.Row.RowType = DataControlRowType.Header Then
  47.             Dim litHeader As Literal
  48.             Dim litHeader1 As Literal
  49.             Dim txtSearch As TextBox
  50.             ' loop through each cell
  51.             For i As Integer = 0 To (e.Row.Cells.Count - 1)
  52.                 If i = 1 Then
  53.                     litHeader = New Literal
  54.                     txtSearch = New TextBox
  55.                     'GridView1.FindControl(txtSearch), = New TextBox
  56.                     ' Dim txt1 As TextBox = CType(GridView1.FindControl("textsearch"), TextBox)
  57.                     With txtSearch
  58.                         '.Text = "fdfdsfd"
  59.                         '.MaxLength = 10
  60.                         .Width = 35
  61.                         '.Text = "rere"
  62.                         .ID = "txtname1"
  63.                     End With
  64.                     ' get the current header text
  65.                     litHeader.Text = e.Row.Cells(1).Text & "<br />"
  66.                     ' add the header text plus a new textbox
  67.                     e.Row.Cells(1).Controls.Add(litHeader)
  68.                     e.Row.Cells(1).Controls.Add(txtSearch)
  69.                 End If
  70.                 If i = 2 Then
  71.                     litHeader1 = New Literal
  72.                     txtSearch1 = New TextBox
  73.                     With txtSearch1
  74.                         '.Text = "fdfdsfd"
  75.                         .Width = 75
  76.                         .ID = "txtname2"
  77.                     End With
  78.                     ' get the current header text
  79.                     litHeader1.Text = e.Row.Cells(2).Text & "<br />"
  80.                     ' add the header text plus a new textbox
  81.                     e.Row.Cells(2).Controls.Add(litHeader1)
  82.                     e.Row.Cells(2).Controls.Add(txtSearch1)
  83.                 End If
  84.                 If i = 3 Then
  85.                     litHeader = New Literal
  86.                     txtSearch2 = New TextBox
  87.                     With txtSearch2
  88.                         '.Text = "fdfdsfd"
  89.                         .Width = 75
  90.                         .ID = "txtname3"
  91.                     End With
  92.                     litHeader.Text = e.Row.Cells(3).Text & "<br />"
  93.                     e.Row.Cells(3).Controls.Add(litHeader)
  94.                     e.Row.Cells(3).Controls.Add(txtSearch2)
  95.                 End If
  96.                 If i = 4 Then
  97.                     litHeader = New Literal
  98.                     txtSearch3 = New TextBox
  99.                     With txtSearch3
  100.                         '.Text = "fdfdsfd"
  101.                         .Width = 75
  102.                         .ID = "txtname4"
  103.                     End With
  104.                     ' get the current header text
  105.                     litHeader.Text = e.Row.Cells(4).Text & "<br />"
  106.                     ' add the header text plus a new textbox
  107.                     e.Row.Cells(4).Controls.Add(litHeader)
  108.                     e.Row.Cells(4).Controls.Add(txtSearch3)
  109.                 End If
  110.                 If i = 5 Then
  111.                     litHeader = New Literal
  112.                     txtSearch4 = New TextBox
  113.                     With txtSearch4
  114.                         '.Text = "fdfdsfd"
  115.                         .Width = 75
  116.                         .ID = "txtname5"
  117.                     End With
  118.                     ' get the current header text
  119.                     litHeader.Text = e.Row.Cells(5).Text & "<br />"
  120.                     ' add the header text plus a new textbox
  121.                     e.Row.Cells(5).Controls.Add(litHeader)
  122.                     e.Row.Cells(5).Controls.Add(txtSearch4)
  123.                 End If
  124.                 If i = 6 Then
  125.                     litHeader = New Literal
  126.                     txtSearch5 = New TextBox
  127.                     With txtSearch5
  128.                         '.Text = "fdfdsfd"
  129.                         .Width = 75
  130.                         .ID = "txtname6"
  131.                     End With
  132.                     ' get the current header text
  133.                     litHeader.Text = e.Row.Cells(6).Text & "<br />"
  134.                     ' add the header text plus a new textbox
  135.                     e.Row.Cells(6).Controls.Add(litHeader)
  136.                     e.Row.Cells(6).Controls.Add(txtSearch5)
  137.                 End If
  138.                 If i = 7 Then
  139.                     litHeader = New Literal
  140.                     txtSearch6 = New TextBox
  141.                     With txtSearch6
  142.                         '.Text = "fdfdsfd"
  143.                         .Width = 75
  144.                         .ID = "txtname7"
  145.                     End With
  146.                     ' get the current header text
  147.                     litHeader.Text = e.Row.Cells(7).Text & "<br />"
  148.                     ' add the header text plus a new textbox
  149.                     e.Row.Cells(7).Controls.Add(litHeader)
  150.                     e.Row.Cells(7).Controls.Add(txtSearch6)
  151.                 End If
  152.             Next
  153.         End If
  154.         'End If
  155.     End Sub
  156.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  157.         Dim txt1 As System.Web.UI.WebControls.TextBox = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname1"), System.Web.UI.WebControls.TextBox)
  158.         Dim z As String = txt1.Text
  159.         Dim txt2 As System.Web.UI.WebControls.TextBox = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname2"), System.Web.UI.WebControls.TextBox)
  160.         Dim v As String = txt2.Text
  161.         Dim txt3 As System.Web.UI.WebControls.TextBox = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname3"), System.Web.UI.WebControls.TextBox)
  162.         Dim vw As String = txt3.Text
  163.         s = "SELECT Id,Label,Textbox FROM textboxtable where Id like '" & txt1.Text.Trim() & "%' and label like '" & txt2.Text.Trim() & "%' and Textbox like '" & txt3.Text.Trim() & "%'"
  164.         'and TaxName like '" & wt2.Text & "%' and MS01User like '" & wt3.Text & "%' and LUDatetime like '" & wt5.text & "%' and SystemGenerated like '" & wt6.Text & "%' and MS04Status like '" & wt7.Text & "%'"
  165.         con.Open()
  166.         da = New SqlDataAdapter(s, con)
  167.         scb = New SqlCommandBuilder(da)
  168.         ds = New DataSet
  169.         da.Fill(ds)
  170.         BulkEditGridView1_1.DataSource = ds.Tables(0).DefaultView
  171.         BulkEditGridView1_1.DataBind()
  172.         con.Close()
  173.     End Sub
  174.     Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  175.         con.Open()
  176.         s = "SELECT Id,Label,Textbox FROM textboxtable"
  177.         da = New SqlDataAdapter(s, con)
  178.         scb = New SqlCommandBuilder(da)
  179.         ds = New DataSet
  180.         da.Fill(ds)
  181.         BulkEditGridView1_1.DataSource = ds.Tables(0).DefaultView
  182.         BulkEditGridView1_1.DataBind()
  183.     End Sub
  184. End Class

i would really appreciate any help on this from ur side....
painthu

Last edited by digioz; 05-21-08 at 11:27 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-20-08, 12:38 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,167
Thanks: 3
Thanked 8 Times in 8 Posts
Quote:
but u see the problem is that in the case of a custom control, the grid view can be populated using any table from sql server...
You could check the dataset to see what table is populating the grid and write a custom query for each table based on that.

Quote:
but it's the search function that i am uanble to incorporate....any ideas....?
Here is an idea. Instead of using queries to search SQL tables, why don't you write a routine to loop through all cells in the search column and inspect the actual text inside the grid for keywords? That way no matter what table is used, you can find matches for your search and clear/repopulate the grid with the matching rows.

Pete
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-20-08, 11:49 PM
painthu painthu is offline
New Member
 
Join Date: May 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Sir,
thank u for the tip...
so u suggest that instead of writing a query that hits the database every time, we need to search from the present grid view itself...k...
but i have no clue how to do that...i mean how to search using a dataset alone...i m trying sir..and in case u find some code or example that could help me create such a routine plz do inform me

i really appreciate your help
painthu
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-21-08, 09:53 AM
painthu painthu is offline
New Member
 
Join Date: May 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
searching with the grid view

sir,
so ur suggestion is to search in the grid itself and not in the database..

that is what i am planning to do.. what i am doing now is that.
i am creating a datatable which will temporarily store the searched result set from the grid view and will be used to populate the grid view
on the click of search button
i pass the original dataset dataset as a parameter to the search function ...inside the search function, i am going to create a datatable that will have exactly the same number of datacolumns as the original table..


i tried with the custom control...no go..
so i decided that i would try with a normal class ,
thenn created an aspx page from which i would call the class's function...including the search function...

i am dissecting the code now

first for generating the textboxes at run time



asp Code:
  1. ]Sub BulkEditGridView1_1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
  2.  
  3.         If e.Row.RowType = DataControlRowType.Header Then
  4.  
  5.             For i As Integer = 0 To (e.Row.Cells.Count - 1)
  6.                 For z As Integer = 1 To (e.Row.Cells.Count - 1)
  7.                     If i = z Then
  8.                         txtSearch = New TextBox
  9.                         txtSearch.ID = "txtname" & z
  10.                         txtSearch.Width = 35
  11.                         e.Row.Cells(z).Controls.Add(txtSearch)
  12.  
  13.                     End If
  14.                 Next
  15.             Next
  16.  
  17.         End If
  18.  
  19.         'End If
  20.     End Sub

created a grid view and 2 buttons on the aspx page...


the search function..
asp Code:
  1. Public Function search(ByVal ds As DataSet, ByVal BulkEditGridView1_1 As GridView) As DataSet
  2.         Dim s As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname1"), System.Web.UI.WebControls.TextBox).Text
  3.  
  4.         'dim s as  = CType(BulkEditGridView1_1.HeaderRow.Controls(0), TextBox).Text
  5.         Dim r As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname2"), System.Web.UI.WebControls.TextBox).Text
  6.         Dim n As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname3"), System.Web.UI.WebControls.TextBox).Text
  7.         Dim str_s, str_r, str_n As String
  8.         Dim dt As DataTable
  9.         Dim dr As DataRow
  10.         Dim dc As DataColumn
  11.         'dc=new DataColumn("
  12.         '  dr = New DataRow()
  13.         '==============
  14.         Dim Table1 As DataTable
  15.         Table1 = New DataTable()
  16.        
  17.         '  Dim Row1, Row2, Row3 As DataRow
  18.                        
  19.         For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
  20.             'Dim str As String = ds.Tables(0).Columns(i).ColumnName
  21.             Dim col As DataColumn
  22.             col = New DataColumn
  23.             col.ColumnName = ds.Tables(0).Columns(i).ColumnName
  24.             col.DataType = GetType(System.String)
  25.             Table1.Columns.Add(col)
  26.             'Table1.Columns(i).ColumnName = ds.Tables(0).Columns(i).ColumnName
  27.         Next
  28.               '============
  29.         Dim dr1 As DataRow
  30.         For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
  31.             str_s = ds.Tables(0).Rows(i).Item(0).ToString
  32.             str_r = ds.Tables(0).Rows(i).Item(1).ToString
  33.             str_n = ds.Tables(0).Rows(i).Item(2).ToString
  34.             dr1 = Table1.NewRow()
  35.             If str_s.StartsWith(s) Then
  36.                 'dr1 = New TableRow
  37.                 dr1.Item(0) = ds.Tables(0).Rows(i).Item(0).ToString
  38.                 dr1.Item(1) = ds.Tables(0).Rows(i).Item(1).ToString
  39.                 dr1.Item(2) = ds.Tables(0).Rows(i).Item(2).ToString
  40.                 '    Table1.Rows.Add(ds.Tables(0).Rows(i))
  41.                 Table1.Rows.Add(dr1)
  42.             End If
  43.             'If str_r.StartsWith(r) Then
  44.             'dt.Rows.Add(ds.Tables(0).Rows(i))
  45.             'End If
  46.         Next
  47.         Dim ds1 As New DataSet
  48.         ds.Tables.Add(Table1)
  49.         Return ds1
  50.      
  51.      
  52.     End Function

...inside the search function, i am going to create a datatable that will have exactly the same number of datacolumns as the original table..

asp Code:
  1. For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
  2.             'Dim str As String = ds.Tables(0).Columns(i).ColumnName
  3.             Dim col As DataColumn
  4.             col = New DataColumn
  5.             col.ColumnName = ds.Tables(0).Columns(i).ColumnName
  6.             col.DataType = GetType(System.String)
  7.             Table1.Columns.Add(col)
  8.             'Table1.Columns(i).ColumnName = ds.Tables(0).Columns(i).ColumnName
  9.  
  10.         Next        'Dim str As String = ds.Tables(0).Columns(0).ColumnName

ds is the original dataset connected directly to the database and is being passed from the aspx code behind-page
asp Code:
  1. Imports System
  2. Imports System.Data
  3.  
  4. Imports System.Data.SqlClient
  5. Partial Class Default3
  6.     Inherits System.Web.UI.Page
  7.     Dim con As New SqlConnection
  8.     Dim s As String
  9.     Dim ds As New DataSet
  10.     Dim da As New SqlDataAdapter
  11.     Dim scb As New SqlCommandBuilder
  12.  
  13.     Dim obj As New Class1
  14.  
  15.  
  16.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  17.         con = New SqlConnection("server=DCPLDC;user id=sa;password=crorepathy;database=Test")
  18.         ' con = New SqlConnection("server=DCPLDC;user id=sa;password=crorepathy;database=Testing")
  19.         s = "select * from Table_1"
  20.         da = New SqlDataAdapter(s, con)
  21.         ds = New DataSet
  22.         scb = New SqlCommandBuilder(da)
  23.         da.Fill(ds)
  24.         'WebCustomControl3_2.DataSource = ds
  25.         'WebCustomControl3_2.DataBind()
  26.         BulkEditGridView1_1.DataSource = ds
  27.         BulkEditGridView1_1.DataBind()
  28.     End Sub
  29.    
  30.     Protected Sub BulkEditGridView1_1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles BulkEditGridView1_1.RowCreated
  31.         obj.BulkEditGridView1_1_RowCreated(sender, e)
  32.     End Sub
  33.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  34.         obj.search(ds, BulkEditGridView1_1)
  35.         ' obj.binddata(x, BulkEditGridView1_1, con)
  36.        
  37.     End Sub
  38.  
  39.     Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  40.         '  obj.binddata("SELECT Id,Label,Textbox  FROM textboxtable", BulkEditGridView1_1, con)
  41.         con = New SqlConnection("server=DCPLDC;user id=sa;password=crorepathy;database=Test")
  42.         ' con = New SqlConnection("server=DCPLDC;user id=sa;password=crorepathy;database=Testing")
  43.         s = "select * from Table_1"
  44.         da = New SqlDataAdapter(s, con)
  45.         ds = New DataSet
  46.         scb = New SqlCommandBuilder(da)
  47.         da.Fill(ds)
  48.         'WebCustomControl3_2.DataSource = ds
  49.         'WebCustomControl3_2.DataBind()
  50.         BulkEditGridView1_1.DataSource = ds
  51.         BulkEditGridView1_1.DataBind()
  52.     End Sub
  53. End Class


i am testing the code in an ordinary class ... i am planning to add it later to the custom control when the code works...



the following is the code i use for the searching

asp Code:
  1. Dim dr1 As DataRow
  2.         For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
  3.             str_s = ds.Tables(0).Rows(i).Item(0).ToString
  4.             str_r = ds.Tables(0).Rows(i).Item(1).ToString
  5.             str_n = ds.Tables(0).Rows(i).Item(2).ToString
  6.  
  7.             dr1 = Table1.NewRow()
  8.             If str_s.StartsWith(s) Then
  9.  
  10.  
  11.                 'dr1 = New TableRow
  12.                 dr1.Item(0) = ds.Tables(0).Rows(i).Item(0).ToString
  13.                 dr1.Item(1) = ds.Tables(0).Rows(i).Item(1).ToString
  14.                 dr1.Item(2) = ds.Tables(0).Rows(i).Item(2).ToString
  15.                 '    Table1.Rows.Add(ds.Tables(0).Rows(i))
  16.                 Table1.Rows.Add(dr1)
  17.  
  18.             End If
  19.             'If str_r.StartsWith(r) Then
  20.             'dt.Rows.Add(ds.Tables(0).Rows(i))
  21.  
  22.  
  23.             'End If
  24.         Next

the search function:-
asp Code:
  1. Public Function search(ByVal ds As DataSet, ByVal BulkEditGridView1_1 As GridView) As DataSet
  2.         Dim s As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname1"), System.Web.UI.WebControls.TextBox).Text
  3.  
  4.         'dim s as  = CType(BulkEditGridView1_1.HeaderRow.Controls(0), TextBox).Text
  5.         Dim r As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname2"), System.Web.UI.WebControls.TextBox).Text
  6.         Dim n As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname3"), System.Web.UI.WebControls.TextBox).Text
  7.         Dim str_s, str_r, str_n As String
  8.         Dim dt As DataTable
  9.         Dim dr As DataRow
  10.         Dim dc As DataColumn
  11.         'dc=new DataColumn("
  12.         '  dr = New DataRow()
  13.         '==============
  14.         Dim Table1 As DataTable
  15.         Table1 = New DataTable()
  16.         'creating a table named Customers
  17.         '  Dim Row1, Row2, Row3 As DataRow
  18.         'declaring three rows for the table
  19.         'Try
  20.         Dim Name As DataColumn = New DataColumn("name")
  21.         '    'declaring a column named Name
  22.         '    Name.DataType = System.Type.GetType("System.String")
  23.         '    'setting the datatype for the column
  24.         '    Table1.Columns.Add(Name)
  25.         '    'adding the column to table
  26.         '    Dim Product As DataColumn = New DataColumn("Product")
  27.         '    Product.DataType = System.Type.GetType("System.String")
  28.         '    Table1.Columns.Add(Product)
  29.         '    Dim Location As DataColumn = New DataColumn("Location")
  30.         '    Location.DataType = System.Type.GetType("System.String")
  31.         '    Table1.Columns.Add(Location)
  32.         'Row1 = Table1.NewRow()
  33.         ''declaring a new row
  34.         'Row1.Item("Name") = "Reddy"
  35.         ''filling the row with values. Item property is used to set the field value.
  36.         'Row1.Item("Product") = "Notebook"
  37.         ''filling the row with values. adding a product
  38.         'Row1.Item("Location") = "Sydney"
  39.         ''filling the row with values. adding a location
  40.         'Table1.Rows.Add(Row1)
  41.         '    'adding the completed row to the table
  42.         '    Row2 = Table1.NewRow()
  43.         '    Row2.Item("Name") = "Bella"
  44.         '    Row2.Item("Product") = "Desktop"
  45.         '    Row2.Item("Location") = "Adelaide"
  46.         '    Table1.Rows.Add(Row2)
  47.         '    Row3 = Table1.NewRow()
  48.         '    Row3.Item("Name") = "Adam"
  49.         '    Row3.Item("Product") = "PDA"
  50.         '    Row3.Item("Location") = "Brisbane"
  51.         '    Table1.Rows.Add(Row3)
  52.         'Catch
  53.         'End Try
  54.         'Dim ds As New DataSet()
  55.         'ds = New DataSet()
  56.         ''creating a dataset
  57.         'ds.Tables.Add(Table1)
  58.         'gv.DataSource = ds
  59.         'gv.DataBind()
  60.      
  61. For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
  62.             'Dim str As String = ds.Tables(0).Columns(i).ColumnName
  63.             Dim col As DataColumn
  64.             col = New DataColumn
  65.             col.ColumnName = ds.Tables(0).Columns(i).ColumnName
  66.             col.DataType = GetType(System.String)
  67.             Table1.Columns.Add(col)
  68.             'Table1.Columns(i).ColumnName = ds.Tables(0).Columns(i).ColumnName
  69.         Next
  70.         'Dim str As String = ds.Tables(0).Columns(0).ColumnName
  71.         '============
  72.         Dim dr1 As DataRow
  73.         For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
  74.             str_s = ds.Tables(0).Rows(i).Item(0).ToString
  75.             str_r = ds.Tables(0).Rows(i).Item(1).ToString
  76.             str_n = ds.Tables(0).Rows(i).Item(2).ToString
  77.             dr1 = Table1.NewRow()
  78.             If str_s.StartsWith(s) Then
  79.                 'dr1 = New TableRow
  80.                 dr1.Item(0) = ds.Tables(0).Rows(i).Item(0).ToString
  81.                 dr1.Item(1) = ds.Tables(0).Rows(i).Item(1).ToString
  82.                 dr1.Item(2) = ds.Tables(0).Rows(i).Item(2).ToString
  83.                 '    Table1.Rows.Add(ds.Tables(0).Rows(i))
  84.                 Table1.Rows.Add(dr1)
  85.             End If
  86.             'If str_r.StartsWith(r) Then
  87.             'dt.Rows.Add(ds.Tables(0).Rows(i))
  88.             'End If
  89.         Next
  90.         Dim ds1 As New DataSet
  91.         ds.Tables.Add(Table1)
  92.         Return ds1
  93.     End Function
so an ordinary class code
asp Code:
  1. Imports Microsoft.VisualBasic
  2.  
  3. Imports System
  4. Imports System.Data
  5.  
  6. Imports System.Data.SqlClient
  7. Public Class Class1
  8.     Inherits System.Web.UI.Page
  9.     Dim con As New SqlConnection
  10.     Dim s As String
  11.     Dim ds As New DataSet
  12.     Dim da As New SqlDataAdapter
  13.     Dim scb As New SqlCommandBuilder
  14.     Public grid As GridView
  15.     Public litHeader As Literal
  16.     Public WithEvents txtSearch As TextBox
  17.     ' Public WithEvents txtSearch As DropDownList
  18.     'Public WithEvents txtSearch1 As TextBox
  19.     'Public WithEvents txtSearch2 As TextBox
  20.     'Public WithEvents txtSearch3 As TextBox
  21.     'Public WithEvents txtSearch4 As TextBox
  22.     'Public WithEvents txtSearch5 As TextBox
  23.     'Public WithEvents txtSearch6 As TextBox
  24.     Public f As String
  25.     Sub BulkEditGridView1_1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
  26.         If e.Row.RowType = DataControlRowType.Header Then
  27.             For i As Integer = 0 To (e.Row.Cells.Count - 1)
  28.                 For z As Integer = 1 To (e.Row.Cells.Count - 1)
  29.                     If i = z Then
  30.                         txtSearch = New TextBox
  31.                         txtSearch.ID = "txtname" & z
  32.                         txtSearch.Width = 35
  33.                         e.Row.Cells(z).Controls.Add(txtSearch)
  34.                     End If
  35.                 Next
  36.             Next
  37.         End If
  38.         'End If
  39.     End Sub
  40.     Public Function search(ByVal ds As DataSet, ByVal BulkEditGridView1_1 As GridView) As DataSet
  41.         Dim s As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname1"), System.Web.UI.WebControls.TextBox).Text
  42.  
  43.         'dim s as  = CType(BulkEditGridView1_1.HeaderRow.Controls(0), TextBox).Text
  44.         Dim r As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname2"), System.Web.UI.WebControls.TextBox).Text
  45.         Dim n As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname3"), System.Web.UI.WebControls.TextBox).Text
  46.         Dim str_s, str_r, str_n As String
  47.         Dim dt As DataTable
  48.         Dim dr As DataRow
  49.         Dim dc As DataColumn
  50.         'dc=new DataColumn("
  51.         '  dr = New DataRow()
  52.         '==============
  53.         Dim Table1 As DataTable
  54.         Table1 = New DataTable()
  55.         'creating a table named Customers
  56.         '  Dim Row1, Row2, Row3 As DataRow
  57.         'declaring three rows for the table
  58.         'Try
  59.         Dim Name As DataColumn = New DataColumn("name")
  60.         '    'declaring a column named Name
  61.         '    Name.DataType = System.Type.GetType("System.String")
  62.         '    'setting the datatype for the column
  63.         '    Table1.Columns.Add(Name)
  64.         '    'adding the column to table
  65.         '    Dim Product As DataColumn = New DataColumn("Product")
  66.         '    Product.DataType = System.Type.GetType("System.String")
  67.         '    Table1.Columns.Add(Product)
  68.         '    Dim Location As DataColumn = New DataColumn("Location")
  69.         '    Location.DataType = System.Type.GetType("System.String")
  70.         '    Table1.Columns.Add(Location)
  71.         'Row1 = Table1.NewRow()
  72.         ''declaring a new row
  73.         'Row1.Item("Name") = "Reddy"
  74.         ''filling the row with values. Item property is used to set the field value.
  75.         'Row1.Item("Product") = "Notebook"
  76.         ''filling the row with values. adding a product
  77.         'Row1.Item("Location") = "Sydney"
  78.         ''filling the row with values. adding a location
  79.         'Table1.Rows.Add(Row1)
  80.         '    'adding the completed row to the table
  81.         '    Row2 = Table1.NewRow()
  82.         '    Row2.Item("Name") = "Bella"
  83.         '    Row2.Item("Product") = "Desktop"
  84.         '    Row2.Item("Location") = "Adelaide"
  85.         '    Table1.Rows.Add(Row2)
  86.         '    Row3 = Table1.NewRow()
  87.         '    Row3.Item("Name") = "Adam"
  88.         '    Row3.Item("Product") = "PDA"
  89.         '    Row3.Item("Location") = "Brisbane"
  90.         '    Table1.Rows.Add(Row3)
  91.         'Catch
  92.         'End Try
  93.         'Dim ds As New DataSet()
  94.         'ds = New DataSet()
  95.         ''creating a dataset
  96.         'ds.Tables.Add(Table1)
  97.         'gv.DataSource = ds
  98.         'gv.DataBind()
  99.         For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
  100.             'Dim str As String = ds.Tables(0).Columns(i).ColumnName
  101.             Dim col As DataColumn
  102.             col = New DataColumn
  103.             col.ColumnName = ds.Tables(0).Columns(i).ColumnName
  104.             col.DataType = GetType(System.String)
  105.             Table1.Columns.Add(col)
  106.             'Table1.Columns(i).ColumnName = ds.Tables(0).Columns(i).ColumnName
  107.         Next
  108.         'Dim str As String = ds.Tables(0).Columns(0).ColumnName
  109.         '============
  110.         Dim dr1 As DataRow
  111.         For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
  112.             str_s = ds.Tables(0).Rows(i).Item(0).ToString
  113.             str_r = ds.Tables(0).Rows(i).Item(1).ToString
  114.             str_n = ds.Tables(0).Rows(i).Item(2).ToString
  115.             dr1 = Table1.NewRow()
  116.             If str_s.StartsWith(s) Then
  117.                 'dr1 = New TableRow
  118.                 dr1.Item(0) = ds.Tables(0).Rows(i).Item(0).ToString
  119.                 dr1.Item(1) = ds.Tables(0).Rows(i).Item(1).ToString
  120.                 dr1.Item(2) = ds.Tables(0).Rows(i).Item(2).ToString
  121.                 '    Table1.Rows.Add(ds.Tables(0).Rows(i))
  122.                 Table1.Rows.Add(dr1)
  123.             End If
  124.             'If str_r.StartsWith(r) Then
  125.             'dt.Rows.Add(ds.Tables(0).Rows(i))
  126.             'End If
  127.         Next
  128.         Dim ds1 As New DataSet
  129.         ds.Tables.Add(Table1)
  130.         Return ds1
  131.         '  binddata(dt, BulkEditGridView1_1)
  132.         '  s = "SELECT Id,Label,Textbox  FROM textboxtable where Id like '" & s & "%' and label like '" & r & "%' and Textbox like '" & n & "%'"
  133.         'Return s
  134.     End Function
  135.     Public Sub binddata(ByVal dt As DataSet, ByVal grid As GridView)
  136.         'da = New SqlDataAdapter(s, con)
  137.         'scb = New SqlCommandBuilder(da)
  138.         'ds = New DataSet
  139.         'da.Fill(ds)
  140.         dt = Me.search(ds, grid)
  141.         grid.DataSource = dt
  142.         grid.DataBind()
  143.     End Sub
  144. End Class
  145. Imports Microsoft.VisualBasic
  146. Imports System
  147. Imports System.Data
  148. Imports System.Data.SqlClient
  149. Public Class Class1
  150.     Inherits System.Web.UI.Page
  151.     Dim con As New SqlConnection
  152.     Dim s As String
  153.     Dim ds As New DataSet
  154.     Dim da As New SqlDataAdapter
  155.     Dim scb As New SqlCommandBuilder
  156.     Public grid As GridView
  157.     Public litHeader As Literal
  158.     Public WithEvents txtSearch As TextBox
  159.     ' Public WithEvents txtSearch As DropDownList
  160.     'Public WithEvents txtSearch1 As TextBox
  161.     'Public WithEvents txtSearch2 As TextBox
  162.     'Public WithEvents txtSearch3 As TextBox
  163.     'Public WithEvents txtSearch4 As TextBox
  164.     'Public WithEvents txtSearch5 As TextBox
  165.     'Public WithEvents txtSearch6 As TextBox
  166.     Public f As String
  167.     Sub BulkEditGridView1_1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
  168.         If e.Row.RowType = DataControlRowType.Header Then
  169.             For i As Integer = 0 To (e.Row.Cells.Count - 1)
  170.                 For z As Integer = 1 To (e.Row.Cells.Count - 1)
  171.                     If i = z Then
  172.                         txtSearch = New TextBox
  173.                         txtSearch.ID = "txtname" & z
  174.                         txtSearch.Width = 35
  175.                         e.Row.Cells(z).Controls.Add(txtSearch)
  176.                     End If
  177.                 Next
  178.             Next
  179.         End If
  180.         'End If
  181.     End Sub
  182.     Public Function search(ByVal ds As DataSet, ByVal BulkEditGridView1_1 As GridView) As DataSet
  183.         Dim s As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname1"), System.Web.UI.WebControls.TextBox).Text
  184.         'dim s as  = CType(BulkEditGridView1_1.HeaderRow.Controls(0), TextBox).Text
  185.         Dim r As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname2"), System.Web.UI.WebControls.TextBox).Text
  186.         Dim n As String = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname3"), System.Web.UI.WebControls.TextBox).Text
  187.         Dim str_s, str_r, str_n As String
  188.         Dim dt As DataTable
  189.         Dim dr As DataRow
  190.         Dim dc As DataColumn
  191.         'dc=new DataColumn("
  192.         '  dr = New DataRow()
  193.         '==============
  194.         Dim Table1 As DataTable
  195.         Table1 = New DataTable()
  196.         'creating a table named Customers
  197.         '  Dim Row1, Row2, Row3 As DataRow
  198.         'declaring three rows for the table
  199.         'Try
  200.         Dim Name As DataColumn = New DataColumn("name")
  201.         '    'declaring a column named Name
  202.         '    Name.DataType = System.Type.GetType("System.String")
  203.         '    'setting the datatype for the column
  204.         '    Table1.Columns.Add(Name)
  205.         '    'adding the column to table
  206.         '    Dim Product As DataColumn = New DataColumn("Product")
  207.         '    Product.DataType = System.Type.GetType("System.String")
  208.         '    Table1.Columns.Add(Product)
  209.         '    Dim Location As DataColumn = New DataColumn("Location")
  210.         '    Location.DataType = System.Type.GetType("System.String")
  211.         '    Table1.Columns.Add(Location)
  212.  
  213.         'Row1 = Table1.NewRow()
  214.         ''declaring a new row
  215.         'Row1.Item("Name") = "Reddy"
  216.         ''filling the row with values. Item property is used to set the field value.
  217.         'Row1.Item("Product") = "Notebook"
  218.         ''filling the row with values. adding a product
  219.         'Row1.Item("Location") = "Sydney"
  220.         ''filling the row with values. adding a location
  221.         'Table1.Rows.Add(Row1)
  222.         '    'adding the completed row to the table
  223.         '    Row2 = Table1.NewRow()
  224.         '    Row2.Item("Name") = "Bella"
  225.         '    Row2.Item("Product") = "Desktop"
  226.         '    Row2.Item("Location") = "Adelaide"
  227.         '    Table1.Rows.Add(Row2)
  228.         '    Row3 = Table1.NewRow()
  229.         '    Row3.Item("Name") = "Adam"
  230.         '    Row3.Item("Product") = "PDA"
  231.         '    Row3.Item("Location") = "Brisbane"
  232.         '    Table1.Rows.Add(Row3)
  233.         'Catch
  234.         'End Try
  235.         'Dim ds As New DataSet()
  236.         'ds = New DataSet()
  237.         ''creating a dataset
  238.         'ds.Tables.Add(Table1)
  239.  
  240.         'gv.DataSource = ds
  241.         'gv.DataBind()
  242.  
  243.  
  244.  
  245.         For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
  246.             'Dim str As String = ds.Tables(0).Columns(i).ColumnName
  247.             Dim col As DataColumn
  248.             col = New DataColumn
  249.             col.ColumnName = ds.Tables(0).Columns(i).ColumnName
  250.             col.DataType = GetType(System.String)
  251.             Table1.Columns.Add(col)
  252.             'Table1.Columns(i).ColumnName = ds.Tables(0).Columns(i).ColumnName
  253.  
  254.         Next
  255.  
  256.         'Dim str As String = ds.Tables(0).Columns(0).ColumnName
  257.         '============
  258.         Dim dr1 As DataRow
  259.         For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
  260.             str_s = ds.Tables(0).Rows(i).Item(0).ToString
  261.             str_r = ds.Tables(0).Rows(i).Item(1).ToString
  262.             str_n = ds.Tables(0).Rows(i).Item(2).ToString
  263.  
  264.             dr1 = Table1.NewRow()
  265.             If str_s.StartsWith(s) Then
  266.  
  267.  
  268.                 'dr1 = New TableRow
  269.                 dr1.Item(0) = ds.Tables(0).Rows(i).Item(0).ToString
  270.                 dr1.Item(1) = ds.Tables(0).Rows(i).Item(1).ToString
  271.                 dr1.Item(2) = ds.Tables(0).Rows(i).Item(2).ToString
  272.                 '    Table1.Rows.Add(ds.Tables(0).Rows(i))
  273.                 Table1.Rows.Add(dr1)
  274.  
  275.             End If
  276.             'If str_r.StartsWith(r) Then
  277.             'dt.Rows.Add(ds.Tables(0).Rows(i))
  278.  
  279.  
  280.             'End If
  281.         Next
  282.         Dim ds1 As New DataSet
  283.         ds.Tables.Add(Table1)
  284.         Return ds1
  285.         '  binddata(dt, BulkEditGridView1_1)
  286.  
  287.         '  s = "SELECT Id,Label,Textbox  FROM textboxtable where Id like '" & s & "%' and label like '" & r & "%' and Textbox like '" & n & "%'"
  288.         'Return s
  289.  
  290.     End Function
  291.     Public Sub binddata(ByVal dt As DataSet, ByVal grid As GridView)
  292.         'da = New SqlDataAdapter(s, con)
  293.         'scb = New SqlCommandBuilder(da)
  294.         'ds = New DataSet
  295.         'da.Fill(ds)
  296.         dt = Me.search(ds, grid)
  297.         grid.DataSource = dt
  298.         grid.DataBind()
  299.     End Sub
  300.  
  301. End Class
  302.  
  303.  
  304. the code is not working the ...when i click the search absolutely nothing happens... course it shows error on the line where i am adding the dr1 to the table1
sir,
any suggestion from ur side is welcome....

painthu

Last edited by Nico; 05-21-08 at 10:13 AM. Reason: [highlight=asp] wrappers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-21-08, 10:14 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
painthu, please use [highlight=vbnet] [/highlight] wrappers when posting ASP code. That makes it a lot easier to read.

http://www.programmingtalk.com/annou...php?f=144&a=14

Thanks.

Last edited by digioz; 05-21-08 at 11:29 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 05-21-08, 11:11 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,167
Thanks: 3
Thanked 8 Times in 8 Posts
Hey painthu,

Excellent! You just saved me half an hour of typing and coding by answering your own question! Yes, that is exactly what I had in mind. I didn't try your code out yet, but you do understand the concept correctly now.


Pete
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting in a custom grid view painthu ASP.NET 1 05-17-08 03:51 PM
Image in a grid view. Rapid Dr3am ASP.NET 3 02-13-08 11:34 AM
Grid View Toggle Detail Rapid Dr3am ASP.NET 1 02-06-08 06:03 AM
Use forum points to view forum post CWY.net Script Requests 0 11-09-06 09:19 PM
visual basic problem I cannot solve: data grid : need vb programmer for hire chrismonroe10 Visual Basic 1 03-25-05 07:42 PM


All times are GMT -5. The time now is 08:10 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.