Advertisement

05.02.2008 at 04:47PM PDT, ID: 23373255
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.9

VB.net 2.0 Windows Forms DataGridView Get Cell Value from Column Name and Row

Asked by jaydwest in Microsoft Visual Basic.Net, .NET Framework 2.x

Tags:

I'm new to VB.net and for months I have been searching for a way to get the value of a datagridview cell using the ColumnName and the Row.  I have been told on this site and several others either that you cannot do it, you can only get the value of a cell by using the cell index, or you can do it simply by referring to the cell by Cells("ColumnName").   After testing these suggestions, neither worked.  

This morning a did read another response on this site that was the best presentation on this subject yet but the author did not think you could do it.  But I really wanted to be able to get the value of a cell using the column name and the row.  Using this article as my starting point, here's what I found

1) You cannot find the cell associated with a DataGridColumn from the DataGridColumn.
2)  You cannot refer to a cell by name.
3)  There is no ordinal relationship between columns and cells, e.g. if column 2 = cell 8 then column 3 may not point to cell 9.
4)  However, you can get the DataGridColumn from a Cell using the OwningColumn property.  

At first the Cell.OwningColumn property seems backward but after thinking about it, the OwningColumn is the Foreign Key to DataGridColumn and the DataGridColumn is the parent of the cell (There are many cells in a Column).

Using this, I was able to create the code to generate a SortedList with the ColumnName as the Sort Field, and the Cell Index as the second field.  After testing this works greate.  I have added the code snippet below.   Essentially this code allows you to write formulas based on column names instead of cell index numbers, for example. (This is a simplified snipet that does not include reference to sorted list)

     TotalCost = Cost1 + Cost2

I would really appreciate any feedback on the algorithm and the code.  I hope this helps others.

PS Can I get some points for answering this question




Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Public dgvCells as New SortedList(Of String, Long)
 
'-- Load Sorted List when you first enter the DataGrid (Not Datagrid must have rows)
If dgvCells.Count = 0 then
   Dim rowCurr as DataGridViewRow = datagridview1.currentrow
   Dim strColumnName as string
   Dim dgvColumn as New datagridviewcolumn
   Dim i as Int16 = 0
   For i = 0 to rowCurr.Cells.count	-1
      dgvColumn  = rowCurr.Cells(i).OwningColumn
      if dgvColumn.IsDataBound then
         strColumnName = dgvColumn.DataPropertyName
      Else
	strColumnName = dgvColumn.name
      End If
	dgvCells.Add(strColumnName, i)
   Next
End If
 
'-- Get the Cell Value by ColumnName
Dim rowCurrent as DataGridViewRow = datagridview1.CurrentRow
		
Dim intColumnIndex as integer  = Cint(dgvCells("CompanyName"))
Dim strCompanyName as string = Cstr(rowCurrent.Cells(intColumnIndex).Value)
[+][-]05.03.2008 at 03:29AM PDT, ID: 21491940

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.03.2008 at 10:10AM PDT, ID: 21493041

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.03.2008 at 10:35AM PDT, ID: 21493130

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.03.2008 at 03:54PM PDT, ID: 21493940

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, .NET Framework 2.x
Tags: VB.net 2.0, Visual Basic 2005, Windows Forms
Sign Up Now!
Solution Provided By: Sancler
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628