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

[SOLVED] searching through a grid view

 
Prev Previous Post   Next Post Next
  #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 TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
 

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 11:52 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.