DataView.Find() after sorting datagrid

G

Guest

I'm getting an "invalid cast" error message when trying do use Dataview.Find() method to get the row I need to update in my datagrid. The problem just occurs after I sort the datagrid by a non default order.

// Update grid code
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
int rowIndex = dv.Find(key);

//Sort Grid code
dv.Sort = e.SortExpression;
DataGrid1.DataBind();
 
M

Miha Markic [MVP C#]

Hi Paulo,

That's because Find searches by sort order columns.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Paulo said:
I'm getting an "invalid cast" error message when trying do use
Dataview.Find() method to get the row I need to update in my datagrid. The
problem just occurs after I sort the datagrid by a non default order.
// Update grid code
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
int rowIndex = dv.Find(key);

//Sort Grid code
dv.Sort = e.SortExpression;
DataGrid1.DataBind();
 
G

Guest

Hi Miha,

What can I do then? How can I solve the problem?

Many thanks, Paulo

Miha Markic said:
Hi Paulo,

That's because Find searches by sort order columns.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Paulo said:
I'm getting an "invalid cast" error message when trying do use
Dataview.Find() method to get the row I need to update in my datagrid. The
problem just occurs after I sort the datagrid by a non default order.
// Update grid code
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
int rowIndex = dv.Find(key);

//Sort Grid code
dv.Sort = e.SortExpression;
DataGrid1.DataBind();
 
M

Miha Markic [MVP C#]

Hi Paulo,

You might create another view that is sorted by your key column.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Paulo said:
Hi Miha,

What can I do then? How can I solve the problem?

Many thanks, Paulo

Miha Markic said:
Hi Paulo,

That's because Find searches by sort order columns.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Paulo said:
I'm getting an "invalid cast" error message when trying do use
Dataview.Find() method to get the row I need to update in my datagrid. The
problem just occurs after I sort the datagrid by a non default order.
// Update grid code
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
int rowIndex = dv.Find(key);

//Sort Grid code
dv.Sort = e.SortExpression;
DataGrid1.DataBind();
 

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