I am trying to make a multiple criteria filter in my datagridview using datatable. Only problem is to convert gridview to table, and was trying more answers found here and MSDN forums. It is still not working and now I'm stuck.
Here is my full method:
public void updateText(string txt, string txt2, string txt3)
{
myform = null;
var bs = (BindingSource)dataGridView1.DataSource;
var table = (DataTable)bs.DataSource;
table.Select("Azonosito Like '%" + txt + "%' AND Név Like '%" + txt2 + "%'");
label4.Text = "Lefutott"; //just for debugging reasons to see if it runs
dataGridView1.DataSource = table;
}
Azonosisto an Név are cloumn headers. txt3 is currently not used, might need it in the future, all filter texts came from another form using this method.
You need to call the DataBind method.
I think that if you add this line in the end (after you assign the data source) it should work.
dataGridView1.DataBind();
Related
That's what it looks like. Student Lookup and Course Lookup search two different databases. I'm trying to display search results from the text boxes on the left. Here's what the database looks like.
This code is ran when a user clicks Submit. I retrieved it from another post but to no avail:
BindingSource bs = new BindingSource();
bs.DataSource = dgvAdvisor.DataSource;
bs.Filter = "First_Name like '%" + lblFirstName.Text + "%' AND
Username like '%" + lblUsername.Text + "%'";
dgvAdvisor.DataSource = bs;
Lines 3 and 4 are one line, Stack Overflow seems to chop it up.
Any advice?
From code you provided, it looks like you are filtering your data source with Label's text, and you want it to be filtered with TextBox's text.
Assuming your associated TextBox controls are txtFirstName and txtUserName ,
try modifying your code as follow.
BindingSource bs = new BindingSource();
bs.DataSource = GetDataTable(); //This method should return whole data table.
bs.Filter = "First_Name like '%" + txtFirstName.Text + "%' AND
Username like '%" + txtUserName.Text + "%'";
dgvAdvisor.DataSource = bs;
I am importing excel file into datagridview then after that I want to perform blind search within these data which maybe imported from one or more file
for now I am filtering data using method below that I am calling it on the Texrbox_TextChanged event
public void DataFilter()
{
BindingSource bs = new BindingSource();
bs.DataSource = DGV_Data.DataSource;
bs.Filter = string.Format(#"convert(National_ID,'System.String') LIKE '%{0}%'
AND First_Name LIKE '%{1}%'
AND Father_Name LIKE '%{2}%'
AND Last_Name LIKE '%{3}%'
AND Mother_Full_Name LIKE '%{4}%'
AND Nationality LIKE '%{5}%'",
TB_National_ID.Text,
TB_First_Name.Text,
TB_Father_Name.Text,
TB_Last_Name.Text,
TB_Mother_Full_Name.Text,
TB_Nationality.Text);
DGV_Data.DataSource = bs;
Re-calculate record counts
L_Rows_Count.Text = "Count: " + (DGV_Data.Rows.Count - 1).ToString("n0");
}
but that will filter data based on the column, so i tried to convert it to blind search as below
public void DataFilter()
{
ExcelRange.get_Range("A1", "XFD1");
Range findRng =
ExcelRange.Find("hello sa",
Type.Missing,
Excel.XlFindLookIn.xlFormulas,
Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlNext,
false,
false,
false);
DGV_Data.DataSource = bs;
Re-calculate record counts
L_Rows_Count.Text = "Count: " + (DGV_Data.Rows.Count - 1).ToString("n0");
}
I am getting error that says
Object reference not set to an instance of an object.
also that will not give back to datagridview all records that may match the text ... I will lose the LIKE functionality
and I don't won't to use ranges cause my code will not be fixable
also any suggestion or better ideas ?
UPDATE
I don't know if that possible to use a sample query with where clause in somehow
cmdtxt = #"select * from [" + ExcelWorkSheets.Name + "$] where somehow ";
I want to filter my datagridview using click event, I'm using the following code:
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView3.DataSource;
bs.Filter = dataGridView3.Columns[0].HeaderText.ToString() + " LIKE '%" + textBox1.Text + "%'";
dataGridView3.DataSource = bs;
This works for my other datagridviews, but it shows an error for this datagridview3: Cannot perform 'Like' operation on System.Int32 and System.String.
Here is the datagridview3 in a screenshot to show which data it is holding:
I guess you have to convert the value you get from textBox1.Text to a number. the data in the first column is numeric, and this comparing text (string) vs a numeric value isn't supported.
Here are some ways of converting to an integer value:
int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);
Once you have succesfully converted, you can adapt your code and try to see if it works that way:
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView3.DataSource;
bs.Filter = dataGridView3.Columns[0].HeaderText.ToString() + " LIKE '%" + Convert.ToInt32(textBox1.Text) + "%'";
dataGridView3.DataSource = bs;
NOTE: Make sure to check whether the use has entered a valid number before hitting the search button ofcourse. Otherwise you'll still receive an error that the value couldn't be parsed/converted to a number.
Hope all of this helps you a bit more.
I am using BindingSource.Filter to filter data on my datagridview. I used the following code:
BindingSource bs = new BindingSource();
bs.DataSource = datagridview1.DataSource;
bs.Filter = "columnName like '%" + textBox1.Text + "%'";
datagridview1.DataSource = bs;
This code works. But when I filter data on a two-word column, the code does not work anymore. I tried putting apostrophe on those words like 'column name' like '%" + tbFilter.Text + "%', but this does not help. Please help me find the right code to filter data on my columns.
Enclose column name in []:
bs.Filter = "[column Name] like '%" + textBox1.Text + "%'";
I think it's always a good idea even if your column names are one-word.
I know this is very basic, but I can't seem to get it right. I have this datatable that I will fill with processed information from my database.
After searching if the EmpRequestID has already been added in the Datatable, I want to be able to get the value of the column named "RequestedEmp" of the returned row and check if already contains the initials that my variable is currently hosting (it is in a loop). If it does not, append the initials in the variable in the existing initials in the row.
DataRow[] MyEmpReq_Row = MyEmpRequests_DataTable.Select("EmpRequestID='" + EmpRequestID + "'");
int SameReqID = MyEmpReq_Row.Length;
if (MyEmpReq_Row > 0) //REQ ID IN DT, MULTIPLE EMP 1 REQUEST
{
//INSERT HERE
}
else //ID NOT IN DT YET
{
MyEmpRequests_DataTable.Rows.Add(EmpRequestID, ActionBy, Requested_Initials, DateSubmitted, RequestStatus);
}
I want to be able to do something like this
string RetrievedInitials = MyEmpReq_Row["RequestedEmp"].ToString();
if (RetrievedInitials LIKE '%" + Requested_Initials + "'") // but if statements doesnt have LIKE
or this and then know if the column contains the value or not.
MyEmpReq_Row.Select("RequestedEmp LIKE '%" + Requested_Initials + "'");
if (RetrievedInitials.Contains(Requested_Initials))
Take a look at the string class:
http://msdn.microsoft.com/en-us/library/system.string.aspx
As already mentioned by some other posters, the Contains() method may be of use. However, you should also look at EndsWith() (which is more in line with your use of the wildcard in your LIKE query) and StartsWith().
Example:
if (RetrievedInitials.EndsWith(Requested_Initials))