469,272 Members | 1,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,272 developers. It's quick & easy.

get the right dataview record

7
I work with .NET2003 C# windows form application, i add a datagrid in my form like this:

Expand|Select|Wrap|Line Numbers
  1. dataGridProduct.DataSource = dataViewProduct;
  2.  
I also make the allowSort property of the datagrid set to be true. Here is my problem, i want to make when i click one of the datagrid cell, said there're ProductID or ProductName, i also show it in my textBoxProductID or textBoxProductName. If i don't have sorting, and i click in the datagrid cell, i can show it by using its MouseUp or MouseDown event. But when i use sorting, which means its columnHeader can be clicked by the user, i get the wrong data of dataView. As my opinion, only the dataView.Sort property is changing, but not the order of dataView's data. How can i view the right data of the cell in the datagrid.

These are my codes inside MouseDown event :
Expand|Select|Wrap|Line Numbers
  1. int currentRowIndex = dataGridProduct.CurrentRowIndex;
  2. DataView dataViewProduct = (DataView)dataGridProduct.DataSource;
  3.  
  4. textBoxProductID.Text = dataViewProduct.Table.Row[currentRowIndex]["ProductID"].ToString();
  5. textBoxProductName.Text = dataViewProduct.Table.Row[currentRowIndex]["ProductName"].ToString();
  6. /* but when i click the columnHeader to get the data sorted, the dataViewProduct rowIndexes are still the same when it was assigned 
  7. (dataViewProduct = getAllProducts();), and i see only the dataViewProduct.Sort is changing. Can i have the dataViewProduct's data also sorted inside it?
  8. */
  9.  
that's all, it's quite long, but i will wait for your reply. Thank you before.
Apr 16 '07 #1
10 3939
Plater
7,872 Expert 4TB
Use the event "SelectionChanged" it will give you a collection of the cells.
Then inside you can check SelectedCells (or something like that) propery of the datagridview and use it to itterate through the various cells the user has selected.
Apr 16 '07 #2
babis
7
Use the event "SelectionChanged" it will give you a collection of the cells.
Then inside you can check SelectedCells (or something like that) propery of the datagridview and use it to itterate through the various cells the user has selected.
i've tried the "CellSelectionChanged", but the parameter "sender" refers to DataGrid itself, and parameter EventArgs "e" referes to something that i can't use. i also try to use dataViewProduct.FindRows((object)productID); and it's successful to find, but i don't know how to catch the value within the dataGridPoduct cell. If it can, at least i can use dataViewProduct.FindRows. If there's another way to do it, please let me know, coz i kinda sick somehow, just because i can't find the way in .NET to do it. Thanks for your attention.
Apr 17 '07 #3
Plater
7,872 Expert 4TB
Say you have a datagridview called dgvList
Expand|Select|Wrap|Line Numbers
  1. if (dgvList.SelectedCells.Count>0)
  2. {
  3.      // access cells by:  dgvList.SelectedCells[0]
  4.     for (int i=0; i <dgvList.SelectedCells.Count;i++)
  5.     {
  6.         if (dgvList.SelectedCells[i].OwningColumn.Name=="MYWANTEDCOLUMN")
  7.         {
  8.           //use this cell
  9.           string myval =dgvList.SelectedCells[i].Value.ToString();
  10.        }
  11.     }
  12. }
  13.  
You can look at the selected cells at any instance you want. Just remember to check to make sure there ARE selected cells first.
Apr 19 '07 #4
babis
7
Dear Plater,
i've tried this, but i don't see the property "Selected Cells" or "OwningColumn" in
dgvList.SelectedCells[i].OwningColumn.Name=="MYWANTEDCOLUMN".

Let me explain the problem once more, i will make it simple now. I have a datagrid with the instance name dataGridProduct, and i take its DataSource from DataView, so its codes like this :
Expand|Select|Wrap|Line Numbers
  1. dataGridProduct.DataSource = dataViewProduct;
and i set "AllowSorting" property of dataGridProduct to true, which means the user can click dataGridProduct column header to sort.

Let me give an example: in the dataViewProduct, i only have one column (its name called "ProductID"), and i have 3 records, say it "1", "2", and "3".
When i click the column header of "ProductID", the grid sort it descending automatically, so it shows "3", "2" and "1". Here's the problem : In dataViewProject.Table.Rows[0]["ProductID"] value is still "1", which means the structure of dataViewProduct is not changed!!! so i can't take the value when the user click the appropriate cell. But the grid show the data sorted descendingly. Can you tell me how to get data the same as dataGridProduct shows?
May 29 '07 #5
Plater
7,872 Expert 4TB
You are using a DataGridView object and not a DataGrid object yes?
My code was for the DataGridView object.

As
Expand|Select|Wrap|Line Numbers
  1. // Summary:
  2. //   Displays ADO.NET data in a scrollable grid. The System.Windows.Forms.DataGridView
  3. //   control replaces and adds functionality to the System.Windows.Forms.DataGrid
  4. //   control; however, the System.Windows.Forms.DataGrid control is retained for
  5. //   both backward compatibility and future use, if you choose.
  6.  
That column sorting only affects how the data is displayed, not the underlying datasource. You must look at the rows/columns of the datagridview object and not your datasource.

EDIT: WOW! No wonder so many people have trouble. I just looked at the DataGrid object and it is horrible. There's like no functionality to it. You all should def make the jump to DataGridView
May 29 '07 #6
babis
7
I'm using a DataGrid Object from System.Windows.Forms.DataGrid. I don't see DataGridView object from my toolbox component... even if i try to look for it from add/remove items - component.

or do you have the *.dll file for DataGridView object, so i can add it manually?
Sorry if i think DataGrid and DataGridView are quite the same, because i'm still a newbie.
maybe i should follow your advise, use the DataGridView object which has more properties and functionalities.Thank you for your attention and your trying on DataGrid object.
May 30 '07 #7
Plater
7,872 Expert 4TB
Hmm, what version of .NET and Visual Studio do you have?
I had to dig to find DataGrid, DataGridView is what came standard for me.

I don't know of any good ways to manipulate the DataGrid object so all I can do is recomend you make the jump to DataGridView (if possible). There is possibly someone else who can help you with DataGrid though
May 30 '07 #8
babis
7
I work with Visual Studio .NET 2003, and its version is .NET 1.1. I use C# programming language and use Windows Application Project. I want to try using DataGridView too, but i don't see it. Or should I work with Visual Studio .NET 2005 (.NET 2.0) --> I don't work with it because its requirements are quite high? thx.
May 30 '07 #9
babis
7
Dear Plater,
I've changed my visual studio .net to 2005, and used .NET 2.0. I've tried for my question to you and it's success, thank you very much for your reply before. But now i've got another problem, i can't dynamically add row to datagridview. i've tried using datagridview.Rows.Add(dataGridViewRow); but it's error (err : "rows cannot be added programatically to the datagridview's collection when control is databound"). I don't know how to add it programatically or dinamically. if you know, please let me know, thx.
Jun 8 '07 #10
Plater
7,872 Expert 4TB
If the object is databound, you must add/remove entries from the source (a datatable or dataset ?). DataTable's have like a "CreateRow()" or something like that
Jun 11 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by TdarTdar | last post: by
8 posts views Thread by Dave Hagerich | last post: by
1 post views Thread by Jay Zweedyk | last post: by
6 posts views Thread by Dazz | last post: by
13 posts views Thread by Steve | last post: by
7 posts views Thread by randy1200 | last post: by
1 post views Thread by glbdev | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.