I work with .NET2003 C# windows form application, i add a datagrid in my form like this: -
dataGridProduct.DataSource = dataViewProduct;
-
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 : -
int currentRowIndex = dataGridProduct.CurrentRowIndex;
-
DataView dataViewProduct = (DataView)dataGridProduct.DataSource;
-
-
textBoxProductID.Text = dataViewProduct.Table.Row[currentRowIndex]["ProductID"].ToString();
-
textBoxProductName.Text = dataViewProduct.Table.Row[currentRowIndex]["ProductName"].ToString();
-
/* but when i click the columnHeader to get the data sorted, the dataViewProduct rowIndexes are still the same when it was assigned
-
(dataViewProduct = getAllProducts();), and i see only the dataViewProduct.Sort is changing. Can i have the dataViewProduct's data also sorted inside it?
-
*/
-
that's all, it's quite long, but i will wait for your reply. Thank you before.
10 4251
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.
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.
Say you have a datagridview called dgvList -
if (dgvList.SelectedCells.Count>0)
-
{
-
// access cells by: dgvList.SelectedCells[0]
-
for (int i=0; i <dgvList.SelectedCells.Count;i++)
-
{
-
if (dgvList.SelectedCells[i].OwningColumn.Name=="MYWANTEDCOLUMN")
-
{
-
//use this cell
-
string myval =dgvList.SelectedCells[i].Value.ToString();
-
}
-
}
-
}
-
You can look at the selected cells at any instance you want. Just remember to check to make sure there ARE selected cells first.
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 : - 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?
You are using a DataGridView object and not a DataGrid object yes?
My code was for the DataGridView object.
As -
// Summary:
-
// Displays ADO.NET data in a scrollable grid. The System.Windows.Forms.DataGridView
-
// control replaces and adds functionality to the System.Windows.Forms.DataGrid
-
// control; however, the System.Windows.Forms.DataGrid control is retained for
-
// both backward compatibility and future use, if you choose.
-
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
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.
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
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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. ...
|
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,...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |