473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

DataGrid search

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

Nov 17 '05 #1
7 5837
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
"Peter" <pc*****@online.nospam> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #2
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.

Nov 17 '05 #3
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");
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:Ie**************@TK2MSFTNGXA01.phx.gbl...
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.

Nov 17 '05 #4
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[i] 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.

Nov 17 '05 #5
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.

Nov 17 '05 #6
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:Rw**************@TK2MSFTNGXA02.phx.gbl...
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!
Nov 17 '05 #7
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.

Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: .Net Sports | last post by:
What is the best way to create a search script with an asp.net calendar in csharp? In visualstudio.net, I want to create a VisualC# project, put a calendar control from the toolbox on the first...
0
by: Matthew Louden | last post by:
I am new to crystal report. I have ASP.NET pages that show the search result. Since the search result is represented by datagrid, it will be nice to put a button on that page, and allows the user...
2
by: HS1 | last post by:
Hello all I use a DataGrid to show the records of a table of an Access database (using a DataSet). Now I want to have a search button in the form so that when there is a match between my...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
1
by: yashpalin | last post by:
i want to search datagrid data withou for loop or any loop is it possible withou it plz if possible give me coding of that
2
by: Gemmalouise1988 | last post by:
hello, i was wondering if it is possible to search a data grid, by typing the criteria in a text box, and pressing a command button to search for it. if it is possible, how would i do it?
0
by: murali81 | last post by:
hi, ive one scenario: i have name,phno,attended status.,and so on.. in table.im getting records into datagrid (in windows application) .i want to search the records based on the name&phno (eg:if i...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
2
Sl1ver
by: Sl1ver | last post by:
Hi there. i got the code for a type of search, incremental if im not mistaken but when i press find, it finds the first instance of what was typed in the textbox but i want it to be like, If you...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.