如何从列表框中获取gridview中的数据 [英] how to fetch data in gridview from listbox

查看:65
本文介绍了如何从列表框中获取gridview中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview和一个列表框.现在我想从具有符号列表的列表框中过滤"symbol"列的数据.Dataview不能正常工作,因为每次它

I have a gridview and a listbox.Now I want to filter the data of column "symbol" from listbox which has a list of symbols.Dataview is not working because each time it
replaces the previous row of gridview.

推荐答案

public void datafetch_of_currdate()//1ST FUNCTION... ................................................... ............
{
如果(this.Page.IsPostBack == false)
{
DataSet ds = new DataSet();
da = new OleDbDataAdapter(选择sr_no,call_time,b_s,符号,entry,exit_sl,exit_target,仪器,curr_status,call_type来自emp,其中curr_date =#" + DateTime.Now.ToShortDateString()+#由sr_no desc排序" cn);
da.Fill(ds,"table");
GridView1.DataSource = ds.Tables [0];
GridView1.DataBind();
ds.Tables [0] .Rows.Clear();
for(int i = 0; i< ListBox1.Items.Count; i ++)
{
foreach(GridView1.Rows中的GridViewRow gr)
{
如果(gr.Cells [3] .Text == ListBox1.Items [i] .Text)
{

DataRow dr = ds.Tables [0] .NewRow();
dr ["sr_no"] = gr.Cells [0] .Text;
dr ["call_time"] = gr.Cells [1] .Text;
dr ["b_s"] = gr.Cells [2] .Text;
dr ["symbol"] = gr.Cells [3] .Text;
dr ["entry"] = gr.Cells [4] .Text;
dr ["exit_sl"] = gr.Cells [5] .Text;
dr ["exit_target"] = gr.Cells [6] .Text;
dr ["instrument"] = gr.Cells [7] .Text;
dr ["curr_status"] = gr.Cells [8] .Text;
dr ["call_type"] = gr.Cells [9] .Text;

ds.Tables [0] .Rows.Add(dr);


}

}
}

GridView1.DataSource = ds.Tables [0];
GridView1.DataBind();
}

}



在表单加载事件中调用此函数.
public void datafetch_of_currdate() //1ST FUNCTION...............................................................................
{
if (this.Page.IsPostBack == false)
{
DataSet ds = new DataSet();
da = new OleDbDataAdapter("select sr_no,call_time,b_s,symbol,entry,exit_sl,exit_target,instrument,curr_status,call_type from emp where curr_date=#" + DateTime.Now.ToShortDateString() + "# order by sr_no desc", cn);
da.Fill(ds, "table");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ds.Tables[0].Rows.Clear();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
foreach (GridViewRow gr in GridView1.Rows)
{
if (gr.Cells[3].Text == ListBox1.Items[i].Text)
{

DataRow dr = ds.Tables[0].NewRow();
dr["sr_no"] = gr.Cells[0].Text;
dr["call_time"] = gr.Cells[1].Text;
dr["b_s"] = gr.Cells[2].Text;
dr["symbol"] = gr.Cells[3].Text;
dr["entry"] = gr.Cells[4].Text;
dr["exit_sl"] = gr.Cells[5].Text;
dr["exit_target"] = gr.Cells[6].Text;
dr["instrument"] = gr.Cells[7].Text;
dr["curr_status"] = gr.Cells[8].Text;
dr["call_type"] = gr.Cells[9].Text;

ds.Tables[0].Rows.Add(dr);


}

}
}

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

}



call this function in form load event.


您可以尝试以下代码:

you can try below code :

 DataSet ds = new DataSet();
 DataTable dt;
 DataTable dtnew;

 OleDbDataAdapter da = new OleDbDataAdapter("Your query", "connection Object");
 da.Fill(ds, "table");
 string strfilterexpretion="";
 // Write a code for get filer expretion like below example
 foreach(ListItem lsitem in ListBox1.Items)
 {
     if(lsitem.Selected )
     {
         if(strfilterexpretion ==string.Empty)
         {

         strfilterexpretion +="''"+lsitem.Text+"''";
         }
         else
         {
         strfilterexpretion +=",''"+lsitem.Text+"''";
         }
     }
}

 // you get selected Row only.
 DataRow[] drs = dt.Select("symbole in (" + strfilterexpretion +")";

 // Create a new table
 dtnew.Rows.Add(drs);

 // Bind the grid view
 GridView1.DataSource = dtnew;
 GridView1.DataBind();




让我知道它是否对您有帮助.




Let me know if it is helpfull to you or not.


这篇关于如何从列表框中获取gridview中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆