DataGrid search

P

Peter

I have data grid and trying to create a search function, the problem I am having is when my table is not sorted in by the primary key I get the closest match in the current order.

Is there a way I can search the DataGrid for exact value and not have it sorted?

Here's my code:

DataTable tbl = this.dvOpenOrders.Table;

object[]findKey = new object[1];

DataColumn[] dcolPk = new DataColumn[1];
dcolPk[0] = tbl.Columns["doc_code"];
tbl.PrimaryKey = dcolPk;
tbl.DefaultView.Sort = "doc_code";
findKey[0] = doc_code;

row = tbl.DefaultView.Find(findKey);

Thanks


Peter
 
I

imperugo

you can use DataView for example:

DataView dv = ds.Tables[0].DefaultView;

//Sort
dv.Sort = "city DESC"

//Filter
dv.RowFilter = "city LiKE '%New York%'"


byez

--
imperugo (exCartman)
myblog : http://imperugo.blogspot.com


I have data grid and trying to create a search function, the problem I am having is when my table is not sorted in by the primary key I get the closest match in the current order.

Is there a way I can search the DataGrid for exact value and not have it sorted?

Here's my code:

DataTable tbl = this.dvOpenOrders.Table;

object[]findKey = new object[1];

DataColumn[] dcolPk = new DataColumn[1];
dcolPk[0] = tbl.Columns["doc_code"];
tbl.PrimaryKey = dcolPk;
tbl.DefaultView.Sort = "doc_code";
findKey[0] = doc_code;

row = tbl.DefaultView.Find(findKey);

Thanks


Peter
 
J

Jeffrey Tan[MSFT]

Hi Peter,

Thanks for your post.

No, to use DataView.Find method, we must first sort the DataView. If you do
not want to do the sort first, I think you may use DataTable.Select method
to implement the search function. In this method parameter, you may use
'column=value' expression to do the search.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Peter

But how do I move the selector in the DataGrid to the found row if the
DataGrid is not sorted by the column I am searching?

here's my code and it does find the row, but what do I with the DataGrid to
move the selector?

DataTable tbl = this.dvOpenOrders.Table;
DataRow [] r = tbl.Select("doc_code=" + doc_code.ToString(), "doc_code");
 
J

Jeffrey Tan[MSFT]

Hi Peter,

Thanks for your feedback.

For the "move the selector", I assume that you mean select certain row in
DataGrid.

Currently, there is no way to get corresponding row in DataGrid to certain
DataRow in datasource. Because in the UI side, the end user may click the
column header to sort and change the row order.

To get what you want, I think we have to re-implement the search function.
We can loop through the DataGrid rows collection, then use
CurrencyManager.List to get the underlying datasource. Sample code listed
below:

private void button1_Click(object sender, System.EventArgs e)
{
for(int i=0;i<this.dataGrid1.VisibleRowCount-1;i++)
{
CurrencyManager cm =
(CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember];
DataRowView drv = cm.List as DataRowView;
DataRow dr=drv.Row;
if(int.Parse(dr["column1"].ToString())==2)
{
this.dataGrid1.Select(i);
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));

for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=5-i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
this.dataGrid1.DataSource=dt;
}
Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Peter,

Does my reply make sense to you? If you still have any concern, please feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Peter

"Jeffrey Tan[MSFT]" said:
Hi Peter,

Does my reply make sense to you? If you still have any concern, please
feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Yes it does, Thank you very much!
 
J

Jeffrey Tan[MSFT]

It's my pleasure to help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top