Question Search and binding data from datagridview

NewGhost

New member
Joined
Apr 30, 2015
Messages
1
Programming Experience
Beginner
I used this code but I only can binding. cann't search, When I have a littel edit, it only search. I want both, How must I do? Thankyou!
[XCODE]privatevoid binding()
{
// luoihang.CurrentRow.Cells["TenHang"].Value;
lbl_iMaDia
.DataBindings.Clear();
lbl_iMaDia
.DataBindings.Add("Text", dgv_MainSearch.DataSource,"MaDia");
lbl_iTenDia
.DataBindings.Clear();
lbl_iTenDia
.DataBindings.Add("Text", dgv_MainSearch.DataSource,"TenDia");
lbl_iNhaSX
.DataBindings.Clear();
lbl_iNhaSX
.DataBindings.Add("Text", dgv_MainSearch.DataSource,"HangSX");
lbl_iTheLoai
.DataBindings.Clear();
lbl_iTheLoai
.DataBindings.Add("Text", dgv_MainSearch.DataSource,"TheLoai");
dtp_iNgayIn
.DataBindings.Clear();
dtp_iNgayIn
.DataBindings.Add("Value", dgv_MainSearch.DataSource,"NgayIn");
lbl_iGiaBan
.DataBindings.Clear();
lbl_iGiaBan
.DataBindings.Add("Text", dgv_MainSearch.DataSource,"Gia");
}

privatevoid quicksearch()
{
string tl ="", ten ="", hang ="";
if(txt_STenDia.Text!="")
{
ten
= txt_STenDia.Text;
}
if(cbo_SLoaiDia.SelectedItem!=null)
{
tl
= cbo_SLoaiDia.SelectedItem.ToString();
}
if(cbo_SHangSX.SelectedItem!=null)
{
hang
= cbo_SHangSX.SelectedItem.ToString();
}
string rule =string.Format("TenDia like '%{0}%' and TheLoai like '%{1}%' and HangSX like'%{2}%'", ten, tl, hang);
dgv_MainSearch
.DataSource=null;
DataRow[] rows = disk.Select(rule);
//MessageBox.Show(rows.Length.ToString());
for(int i =0; i < rows.Length; i++)
dgv_MainSearch
.Rows.Add(rows[i][0], rows[i][1], rows[i][2], rows[i][3], rows[i][4], rows[i][5]);
}privatevoid frm_Main_Load(object sender,EventArgs e)
{
cls_Includes
Inc=new cls_Includes();
disk
=Inc.getdisk();
DataTable ncc =Inc.getproviders();
for(int i =0; i < ncc.Rows.Count; i++)
cbo_SHangSX
.Items.Add(ncc.Rows[i][1]);
DataTable theloai =Inc.gettype();
for(int i =0; i < theloai.Rows.Count; i++)
cbo_SLoaiDia
.Items.Add(theloai.Rows[i][1]);
//dgv_MainSearch.ColumnCount = 6;
//dgv_MainSearch.Columns[0].Name = "MaDia";
//dgv_MainSearch.Columns[1].Name = "TenDia";
//dgv_MainSearch.Columns[2].Name = "TheLoai";
//dgv_MainSearch.Columns[3].Name = "HangSX";
//dgv_MainSearch.Columns[4].Name = "NgayIn";
//dgv_MainSearch.Columns[5].Name = "Gia";
//for (int i = 0; i < disk.Rows.Count; i++)
// dgv_MainSearch.Rows.Add(disk.Rows[0], disk.Rows[1], disk.Rows[2], disk.Rows[3], disk.Rows[4], disk.Rows[5]);
dgv_MainSearch
.DataSource= disk;
binding
();
}[/XCODE]
 
Your code is hard to read because you've butchered the formatting. In future, please paste code as PLAIN TEXT and set the option for your formatting tags properly, i.e.

[xcode=c#]your code here[/xcode]

As far as I can tell, the issue is that you're not searching properly. Once you bind the grid, it remains bound. You don't change the DataSource. You bind your DataRable to a BindingSource and bind that to the grid, then simply set the Filter property of the BindingSource when you want to filter the data:
myBindingSource.DataSource = myDataTable;
myDataGridView.DataSource = myBindingSource;
myBindingSource.Filter = string.Format("TenDia like '%{0}%' and TheLoai like '%{1}%' and HangSX like'%{2}%'", ten, tl, hang);
 

Latest posts

Back
Top Bottom