Filtering DataGridView

Hi!

I have a dataGridView which is bounded to specific dataTable. I want to filter this dataGridView to show those rows which one of its fields starts with specific code but this field is saved on DataBase and also DataTable as an "int". as an example i have a field named bookCode and i want to filter rows which their bookCodes start with 2 and the bookCode field is saved as an int. how can I do that? I know if it was saved as string I could have used something like this: dataTable1.DefaultView.RowFilter = "bookCode like ' " + code +" '%"; but its not so...

Thanks!

[624 byte] By [gygulance] at [2008-1-10]
# 1

Hi!

I have a dataGridView which is bounded to specific dataTable. I want to filter this dataGridView to show those rows which one of its fields starts with specific code but this field is saved on DataBase and also DataTable as an "int". as an example i have a field named bookCode and i want to filter rows which their bookCodes start with 2 and the bookCode field is saved as an int. how can I do that? I know if it was saved as string I could have used something like this: dataTable1.DefaultView.RowFilter = "bookCode like ' " + code +" '%"; but its not so...

Thanks!


gygulance at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Why is starting with 2 significant? Do you want to select where the values are 20-29, 200-299, 2000-2999, etc? Or are they really in a single range such as 200-299? If the latter, just use "bookCode >= 200 AND bookCode < 300" for example.
eradicator at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Thanks, but I its not really like this, 2 was example,it can be any number with any digits and any number of digits

as an example it can be 3 , 45 ,981 no matter see following example:

whenever I type a number in textBox the dataGridView sould be filtered based on "bId" column and number I enter,

if I type 5 all books starting 5 in their "bId" coulmn should be filtered and showed, then in rest if I type 4 and so number in textBox becomes 54 and dataGridView should be filtered based on books which their "bId" numbers start with 54 and so on... remember the bId is saved on DataBase as int.

Code Snippet
gygulance at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Hi, gygulance,

Based on my understanding, you want to filter an int Column like a string Column, don't you?

I think there could be 2 solutions.

First, you can use the integrated Convert method of RowFilter, and convert the int type to string in RowFilter.

For example

Code Block

DataTable datatable;

private void Form1_Load(object sender, EventArgs e)

{

datatable = new DataTable();

datatable.Columns.Add("col1",typeof(int));

datatable.Rows.Add(1);

datatable.Rows.Add(2);

datatable.Rows.Add(23);

datatable.Rows.Add(22);

datatable.Rows.Add(25);

datatable.Rows.Add(72);

dataGridView1.DataSource = datatable.DefaultView;

}

private void button1_Click(object sender, EventArgs e)

{

datatable.DefaultView.RowFilter = "Convert(col1,'System.String') Like '2%'";

}

Second, it is also possible if you add a company column next to the int Column.

For example

Code Block

DataTable datatable;

private void Form1_Load(object sender, EventArgs e)

{

datatable = new DataTable();

datatable.Columns.Add("col1",typeof(int));

datatable.Rows.Add(1);

datatable.Rows.Add(2);

datatable.Rows.Add(22);

datatable.Rows.Add(242);

datatable.Rows.Add(27);

datatable.Rows.Add(72); //We get int Column here.

datatable.Columns.Add("col2", typeof(string)); //String column with the same value

datatable.Columns["col2"].Expression = "col1";

dataGridView1.DataSource = datatable.DefaultView;

}

private void button1_Click(object sender, EventArgs e)

{

datatable.DefaultView.RowFilter = "col2 like '2%'";

}

Hope this helps,

Regards

YuGuo–MSFT at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...