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 textBoxProductI D or textBoxProductN ame. 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 4295 Plater 7,872
Recognized Expert Expert
Use the event "SelectionChang ed" 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 "SelectionChang ed" 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 "CellSelectionC hanged", 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((obje ct)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.
Plater 7,872
Recognized Expert Expert
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 "OwningColu mn" in
dgvList.Selecte dCells[i].OwningColumn.N ame=="MYWANTEDC OLUMN".
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 "AllowSorti ng" 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?
Plater 7,872
Recognized Expert Expert
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.
Plater 7,872
Recognized Expert Expert
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.Ro ws.Add(dataGrid ViewRow); 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.
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
execute the Update method on an adapter for the table that the DataView
is based on, the new record will indeed be written to the database. But
if I call AcceptChanges prior to executing the Update method, the
RowState for the new record is...
|
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 of that table with the insert update delete option selected.
I then put a dataview and attached it to that 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 = "'Record ID' = '0'";
theView.RowStateFilter = DataViewRowState.ModifiedCurrent;
Debug.WriteLine(string.Format("RowFilter = {0}", theView.RowFilter));
RecordDataGrid.DataSource = theView;
RecordDataGrid.DataBind();
|
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 thought the Dataview and update the original dataset .... if there is a
way to index the dataset from the dataview. I know I can update the
|
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 that record number. I
don't however want a filter as the user must still be able to scroll up and
down the DataView.
Thanks. Any help would be greatly appreciated.
| |
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 the confusion.........
I thought that the DataView is the view from the dataset, but it seems that
the dataview has the records that are in the datagrid, because everytime I
search for a record that I know is NOT in the dataset, it finds it. I...
|
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 a subset from the orders table, I need to take the customer
ids in the orders table subset, and list each record in the customer table
that has a matching customer id.
If I were in t-sql, this would be an easy inner join. In this case, I get
|
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.
Both grids have Dataviews as DataSource.
The child grid, filtered with RowFilter in the DataView works fine.
|
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. The DataView shows the
company details and you can delete a record, if necessary. If you
change the selection on the dropdown list, the DataView is refreshed.
This works fine. The problem I am having is that if I delete the
record on the...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |