473,385 Members | 2,003 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,385 software developers and data experts.

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 4251
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

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

Similar topics

1
by: Johann Blake | last post by:
I'm not sure that I entirely understand the purpose of the AcceptChanges methon in a DataView. I added a new record to a DataView and noticed that its RowState indicated it as being "Added". If I...
0
by: TdarTdar | last post by:
Hello, I was wondering if there is a way to used a dataview like the following and how to do it. I have a new table, no records. I have a new asp.net 2.0 beta 2 webform, with a sqldatasource...
8
by: Dave Hagerich | last post by:
I'm using a DataGrid with a DataSet and I'm trying to filter the data being displayed, using the following code as a test: DataView theView = new DataView(theDataSet.Tables); theView.RowFilter =...
1
by: Jay Zweedyk | last post by:
Ok I want to filter a dataset into a dataview and be able to reference back to the dataset from the filtered dataview. Example: 100 record dataset filter it to a 5 record dataview loop...
6
by: Dazz | last post by:
Hi I desperately need some help. I want to have a DataView sorted alphabetically by a specific column, then I want to find the first record that begins with the letter the user inputs and get...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
7
by: randy1200 | last post by:
I have an orders table. Each record in the orders table contains a customer id. I have a customer table. The primary key of each record in the customer table is the customer id. After getting...
2
by: HansP | last post by:
Hi, I am really in a deadlock with this problem. In my VS2005 C# handheld application, I have a grid with Workorders. Another grid (child) are the ACTIONS, performed for each workorder. ...
1
by: glbdev | last post by:
I searched around but cannot find an answer to this problem, if there is an answer. I have a DataView and a dropdownlist both on the same page. The dropdown list is a bound list of companies. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.