painthu 0 Newbie Poster

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 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 unable to incorporate....any ideas....?

the code for the custom control is:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace customcon
{



public class WebCustomControl6 : System.Web.UI.WebControls.GridView
{
SqlConnection con = new SqlConnection();
string s;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder();
public GridView grid;
public DropDownList txtSearch;
public TextBox txtSearch1;
public TextBox txtSearch2;
public TextBox txtSearch3;
public TextBox txtSearch4;
public TextBox txtSearch5;
public TextBox txtSearch6;
public string f;


protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (e.Row.RowType == DataControlRowType.Header)
{
Literal litHeader;
TextBox txtSearch;



//GridViewRow se=new GridViewRow(0,1,DataControlRowType.Header,


for(int i = 0; i <= (e.Row.Cells.Count - 1); i++)
{
for (int z = 0; z <= (e.Row.Cells.Count - 1); z++)
{
if (i == z)
{
litHeader = new Literal();
txtSearch = new TextBox();
txtSearch.ID = "txtname" + z;
txtSearch.Width = 35;
litHeader.Text = e.Row.Cells[z].Text + "<br />";
e.Row.Cells[z].Controls.Add(litHeader);
e.Row.Cells[z].Controls.Add(txtSearch);


}
}
}


}
}
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);


this.CreateSecondHeader();


}
private SqlConnection connection
{
get
{
return (SqlConnection)this.ViewState["connection"];


}
set
{
this.ViewState["connection"] = value;
}


}
private void btnclick(DataTable dt,SqlConnection connection)
{
string s = "select * from " + dt + "where" + this.Columns[0].HeaderText + "like" + "'" + txtSearch.Text + "'";
da = new SqlDataAdapter(s, connection);
scb = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds);
ds.Tables.Add(dt);
this.DataSource = dt;
this.DataBind();


}


private void CreateSecondHeader()
{
GridViewRow row = new GridViewRow(0,1, DataControlRowType.DataRow, DataControlRowState.Normal);


Button btn = new Button();
btn.Width = Unit.Pixel(60);
btn.Text = "search";
Button btn1 = new Button();
btn1.Width = Unit.Pixel(60);
btn1.Text = "cancel";


TableCell th = new TableCell();
th.Controls.Add(btn);


th.HorizontalAlign = HorizontalAlign.Center;
th.ColumnSpan = this.Columns.Count;
TableCell th1 = new TableCell();
th1.Controls.Add(btn1);
th.HorizontalAlign = HorizontalAlign.Center;
th.ColumnSpan = this.Columns.Count;
//th.Width =this.Width;


row.Cells.Add(th);
row.Cells.Add(th1);


this.InnerTable1.Rows.AddAt(0, row);
}
protected Table InnerTable1
{
get
{
if (this.HasControls())
{
return (Table)this.Controls[0];
}


return null;
}
}


}
}


thw code for the code behind...mean under normal circumstances ie with an ordinary grid control---not through  the custom control----



Imports System
Imports System.Data


Imports System.Data.SqlClient
Partial Class Default2
Inherits System.Web.UI.Page
Dim con As New SqlConnection
Dim s As String
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim scb As New SqlCommandBuilder
Dim dt As New DataTable


Public WithEvents txtSearch As DropDownList
Public WithEvents txtSearch1 As TextBox
Public WithEvents txtSearch2 As TextBox
Public WithEvents txtSearch3 As TextBox
Public WithEvents txtSearch4 As TextBox
Public WithEvents txtSearch5 As TextBox
Public WithEvents txtSearch6 As TextBox
Public f As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New SqlConnection("server=DCPLDC;user id=sa;password=crorepathy;database=Testing")
con.Open()


s = "SELECT Id,Label,Textbox  FROM textboxtable"
da = New SqlDataAdapter(s, con)
ds = New DataSet
scb = New SqlCommandBuilder(da)
da.Fill(ds)
ds.Tables.Add(dt)
dt = New DataTable("textboxtable")
BulkEditGridView1_2.DataSource = ds.Tables(0)
BulkEditGridView1_2.DataBind()


con.Close()


End Sub
Sub binddata()



s = "SELECT Id,Label,Textbox  FROM textboxtable"
da = New SqlDataAdapter(s, con)
ds = New DataSet
scb = New SqlCommandBuilder(da)
da.Fill(ds)
ds.Tables.Add(dt)
dt = New DataTable("textboxtable")
BulkEditGridView1_1.DataSource = ds.Tables(0)
BulkEditGridView1_1.DataBind()
End Sub



Protected Sub BulkEditGridView1_1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles BulkEditGridView1_1.RowCreated


If e.Row.RowType = DataControlRowType.Header Then
Dim litHeader As Literal
Dim litHeader1 As Literal
Dim txtSearch As TextBox


' loop through each cell
For i As Integer = 0 To (e.Row.Cells.Count - 1)
If i = 1 Then
litHeader = New Literal
txtSearch = New TextBox
'GridView1.FindControl(txtSearch), = New TextBox
' Dim txt1 As TextBox = CType(GridView1.FindControl("textsearch"), TextBox)


With txtSearch
'.Text = "fdfdsfd"
'.MaxLength = 10
.Width = 35
'.Text = "rere"



.ID = "txtname1"
End With
' get the current header text
litHeader.Text = e.Row.Cells(1).Text & "<br />"


' add the header text plus a new textbox
e.Row.Cells(1).Controls.Add(litHeader)
e.Row.Cells(1).Controls.Add(txtSearch)


End If
If i = 2 Then
litHeader1 = New Literal
txtSearch1 = New TextBox
With txtSearch1
'.Text = "fdfdsfd"
.Width = 75
.ID = "txtname2"
End With
' get the current header text
litHeader1.Text = e.Row.Cells(2).Text & "<br />"


' add the header text plus a new textbox
e.Row.Cells(2).Controls.Add(litHeader1)
e.Row.Cells(2).Controls.Add(txtSearch1)
End If
If i = 3 Then
litHeader = New Literal
txtSearch2 = New TextBox
With txtSearch2
'.Text = "fdfdsfd"
.Width = 75
.ID = "txtname3"


End With


litHeader.Text = e.Row.Cells(3).Text & "<br />"



e.Row.Cells(3).Controls.Add(litHeader)
e.Row.Cells(3).Controls.Add(txtSearch2)
End If
If i = 4 Then
litHeader = New Literal
txtSearch3 = New TextBox
With txtSearch3
'.Text = "fdfdsfd"
.Width = 75
.ID = "txtname4"


End With
' get the current header text
litHeader.Text = e.Row.Cells(4).Text & "<br />"


' add the header text plus a new textbox
e.Row.Cells(4).Controls.Add(litHeader)
e.Row.Cells(4).Controls.Add(txtSearch3)
End If
If i = 5 Then
litHeader = New Literal
txtSearch4 = New TextBox
With txtSearch4
'.Text = "fdfdsfd"
.Width = 75
.ID = "txtname5"


End With
' get the current header text
litHeader.Text = e.Row.Cells(5).Text & "<br />"


' add the header text plus a new textbox
e.Row.Cells(5).Controls.Add(litHeader)
e.Row.Cells(5).Controls.Add(txtSearch4)
End If
If i = 6 Then
litHeader = New Literal
txtSearch5 = New TextBox
With txtSearch5
'.Text = "fdfdsfd"
.Width = 75
.ID = "txtname6"


End With
' get the current header text
litHeader.Text = e.Row.Cells(6).Text & "<br />"


' add the header text plus a new textbox
e.Row.Cells(6).Controls.Add(litHeader)
e.Row.Cells(6).Controls.Add(txtSearch5)
End If
If i = 7 Then
litHeader = New Literal
txtSearch6 = New TextBox
With txtSearch6
'.Text = "fdfdsfd"
.Width = 75
.ID = "txtname7"


End With
' get the current header text
litHeader.Text = e.Row.Cells(7).Text & "<br />"


' add the header text plus a new textbox
e.Row.Cells(7).Controls.Add(litHeader)
e.Row.Cells(7).Controls.Add(txtSearch6)
End If
Next


End If


'End If
End Sub


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim txt1 As System.Web.UI.WebControls.TextBox = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname1"), System.Web.UI.WebControls.TextBox)
Dim z As String = txt1.Text
Dim txt2 As System.Web.UI.WebControls.TextBox = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname2"), System.Web.UI.WebControls.TextBox)
Dim v As String = txt2.Text
Dim txt3 As System.Web.UI.WebControls.TextBox = CType(BulkEditGridView1_1.HeaderRow.FindControl("txtname3"), System.Web.UI.WebControls.TextBox)
Dim vw As String = txt3.Text
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() & "%'"
'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 & "%'"
con.Open()
da = New SqlDataAdapter(s, con)
scb = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds)
BulkEditGridView1_1.DataSource = ds.Tables(0).DefaultView
BulkEditGridView1_1.DataBind()


con.Close()
End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click


con.Open()


s = "SELECT Id,Label,Textbox  FROM textboxtable"


da = New SqlDataAdapter(s, con)
scb = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds)
BulkEditGridView1_1.DataSource = ds.Tables(0).DefaultView
BulkEditGridView1_1.DataBind()


End Sub
End Class

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