开发者

filter to a gridview in c#

I Have a gridview having column names(batchname,firstname,schedule,startdate,enddate,labname)

I want to add a filter to a gridview. Can I add a filter bellow each column of the gridview header.Or Should i use it outside the gridview? Here I m using it outside the gridview.And fire a event of TextChanged.I have done following code but it doesnt work anymore.Please Suggest a solution.

I have to access data from three tables to show in gridview.The three table formats are as below,

BatchMaster(batchmasterid,usermasterid,labmasterid,batchname,batchcode,batchdays,startdate,enddate)

UserMaster(usermasterid,title,firstname,middlename,lastname,phoneno);

LabMaster(labmasterid,labname);

I write a code on textbox change event whitch is taken outside the gridview.

protected void txtSearch_TextChanged(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection("Data
Source=.\\SQLEXPRESS;Initial Catalog=EMS;User ID=sa;Password=sa123");
           string query = "SELECT
batchmasterid,usermasterid,labmasterid,batchname,faculty,batchdays,startdate,enddate,LabMaster.labname
FROM BatchMaster,UserMaster Where batchname LIKE '%" + txtSearch.Text
+ "%'";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           gvHeadload.DataSource = ds;
           gvHeadload.DataBind();

      }

also I have tried a Join Query. But it doesnt work anymore. Pls suggest a solution as early as possible. advanced thank 开发者_StackOverflowu for ur suggation..


First thing, you should not create sql connection every time user types something. You should do it, for example after OnInitialize(). Second, you must open the connection: con.Open(), get data, store it in data set and close it con.Close(). Then you should filter the data set on text changed. There are also other ways of doing it, but I think this will be a good start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜