473,396 Members | 1,840 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,396 software developers and data experts.

How to get the row index of a datagrid in window forms

I can get the row indexes using the following codes. but problem is
that when I clicked the grid column header , it still printed out the
same indexes. So the dataview here reflects the row indexes in the
data source instead of the datagrid. What I want are the row indexes
in the datagrid. How can I do it? Thanks.

private void PrintRowIndexes(DataGrid dg)
{
CurrencyManager cm =
(CurrencyManager)this.BindingContext[dg.DataSource, dg.DataMember];
DataView dv = (DataView)cm.List;
Console.Write ("Indexes: " );
for(int i = 0; i < dv.Count; ++i)
{
if(Convert.ToString (dv[i]["yes_no"]) == "no") {

Console.Write ("{0}\t", i);
}
}
Console.WriteLine ();
}

These always printed the same set of indexes no matter which column
headers I clicked (I called it from datagrid_MouseDown event).
Nov 20 '05 #1
4 1007
Henry,

Your code looks quite correct. Reordering in the grid is actually
facilitated by the underlying DataView the CurrencyManager holds reference
to. You can check that by inspecting the dataview's Sort property value.

What could be a problem that the grid still hasn't had a chance to carry out
the reorder when you call the PrintRowIndexes method. Try handling the
MouseUp event instead and see what happens.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Henry" <we**********@yahoo.com> wrote in message
news:b6**************************@posting.google.c om...
I can get the row indexes using the following codes. but problem is
that when I clicked the grid column header , it still printed out the
same indexes. So the dataview here reflects the row indexes in the
data source instead of the datagrid. What I want are the row indexes
in the datagrid. How can I do it? Thanks.

private void PrintRowIndexes(DataGrid dg)
{
CurrencyManager cm =
(CurrencyManager)this.BindingContext[dg.DataSource, dg.DataMember];
DataView dv = (DataView)cm.List;
Console.Write ("Indexes: " );
for(int i = 0; i < dv.Count; ++i)
{
if(Convert.ToString (dv[i]["yes_no"]) == "no") {

Console.Write ("{0}\t", i);
}
}
Console.WriteLine ();
}

These always printed the same set of indexes no matter which column
headers I clicked (I called it from datagrid_MouseDown event).


Nov 20 '05 #2
I have meet the same problem as you. You should retrive data from the
datagrid not DataView.

for(int i = 0; i < dv.Count; ++i)
{

// assure the datagrid1 is the object of your DataGrid
if(Convert.ToString ( datagrid1[I,¡±yes_no¡±]) == "no") {

Console.Write ("{0}\t", i);
}
}
Nov 20 '05 #3
Using dg[rowidx, colindx] or dataview[rowindx][colname] do the same.
Now the problem is that the printout indexes are the ones before
clicking even I used mouseUp event.

What did I go wrong?

Thanks
"BlueFox" <li*******@boco.com.cn.dicuss> wrote in message news:<O7**************@TK2MSFTNGP11.phx.gbl>...
I have meet the same problem as you. You should retrive data from the
datagrid not DataView.

for(int i = 0; i < dv.Count; ++i)
{

// assure the datagrid1 is the object of your DataGrid
if(Convert.ToString ( datagrid1[I,¡±yes_no¡±]) == "no") {

Console.Write ("{0}\t", i);
}
}

Nov 20 '05 #4
Helle I'm french!!!!
I have a problem to reordes my rows in my datagrid.Indeed I want to drowp this lines
1 ---> 6
6 ---> 2
4 ---> 3
3 --->1
2 ---> 5
5 ---> 4

I USe datragrid Windows Form
Thanks
PS : Excuse me for my english.
Nov 20 '05 #5

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

Similar topics

0
by: Eugene | last post by:
Hello all, I've been trying to figure this out for a few days now, and still have no clue what's going on... I have a few related tables in MS Access (Clients, Cars, Sales), and a datagrid,...
0
by: Dave Elliott | last post by:
After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is after the newly added row without getting bizarre results. I have added the...
4
by: Henry | last post by:
I can get the row indexes using the following codes. but problem is that when I clicked the grid column header , it still printed out the same indexes. So the dataview here reflects the row indexes...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
5
by: sdbranum | last post by:
I have been using Visual C#.NET to code a large project having many data adapters, data sets, datagrids, multiple forms with tab pages, each containing various controls (mostly label, text boxes,...
0
by: Dave | last post by:
Tried posting in the Winform Forum without much luck, so posting here... After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is...
5
by: Genojoe | last post by:
I am using code from Help with two exceptions. (1) I increased the number of sample rows from 3 to 20, and (2) I anchored the datagrid to bottom of form so that I can change the size of the grid by...
0
by: =?Utf-8?B?Q3ltZXI=?= | last post by:
When the datagrid has multi rows and the vertical scrollbar hasn't appeared, if I click the column near the bound or drag horizontal scrollbar, this exception will be throwed. The trace stack as...
3
by: =?Utf-8?B?Q3ltZXI=?= | last post by:
When the datagrid has multi rows and the vertical scrollbar hasn't appeared, if I click the column near the bound or drag horizontal scrollbar, this exception will be throwed. The trace stack as...
3
by: ppkm | last post by:
Hi, I am struck with the same probelm could you please suggest any solution if you found any
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.