Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Ok. So what i am trying to do is

1. Get all cells that are under the third column
2. Get the cell values
3. If any of the cells contain the number "1" i want to change it to name "Free"
4. If any of the cells contain the number "2" i want to change it to name "Paid"
5. If any of the cells contain the number "3" i want to change it to name "Pending"
5. If any of the cells contain the number "4" i want to change it to name "Complete"
4. I want this to occur on all cells below the third column only


So far i think i have managed to get all the cell information put into a variable (i think). Below is the code (This code is me trying to just change one of the cells)
VB
  Dim a As Integer = 1
  Dim a1 As String = "Free"
  Dim cellValues As New List(Of Object)
        For Each row As DataGridViewRow In DGStateInfo.Rows
            cellValues.Add(row.Cells(2).Value)
        Next

        If cellValues.Contains(a) Then
            cellValues.Contains(a1)
        End If




'This is where i get confused

        If cellValues.Contains(a) Then
            cellValues.Contains(a1)
        End If


To be honest i'm not even sure if i'm going about this the right way. This is going on my second week of trying different code with no luck.

I do know that you can use the DataGridView1.Insert(0, "Free") command but the problem is that the data that is loading into the datgridview with have different data in different cells at every load of the program.


I have also tried this

VB
Dim value As Integer

        For Each row As DataGridViewRow In DataGridView1.DataSource
            DataGridView1.Item(0, 1).Value = value
        Next

        If Value.text = 1 than
        value = "free"

            
        End If



And this

VB
Dim cellValues As New List(Of Object)
For i = 0 To DataGridView1.Rows.Count - 1
    cellValues.Add(DataGridView1(0, i).Value)
Next


Thanks in advanced for the help!
Posted
Updated 1-Oct-13 11:41am
v4
Comments
Homero Rivera 1-Oct-13 20:47pm    
Don't really like my current reputation number, so I'll just say 'Hi'.
Homero Rivera 1-Oct-13 20:55pm    
Hey, one sec! Are you developing a WebSite on ASP.Net? Or is this Windows Forms?

1 solution

My bad. This only works in ASP.Net.
And this question is about Windows Forms.
In case anyone is interested, the following works for ASP.Net

VB
For Each row As GridViewRow In DGStateInfo.Rows

   If row.Cells(2).Text = "1" Then
      row.Cells(2).Text = "Free"
   ElseIf row.Cells(2).Text = "2" Then...
      'Etc...
      'Etc...
      'Etc...
   End If

Next


row.Cells(x).Text is the property you need to check and then change.
 
Share this answer
 
v2
Comments
Member 10272248 3-Oct-13 17:24pm    
Wow. So simple. Thanks!
I had to use row.Cells(2).Value = "" instead of .text


However I am having one last problem. When trying to insert the text into the cell. It is giving me a "System.Exception "Free" is not a valid value for byte....." I am assuming this is because the column i am grabbing is set as a byte. How do i set cells in column as string?
Homero Rivera 3-Oct-13 17:28pm    
Are you developing a WebSite on ASP.Net? Or is this Windows Forms?
Member 10272248 3-Oct-13 17:29pm    
Windows forms. VB.NET Visualstudio 2012
Homero Rivera 3-Oct-13 17:37pm    
In that case, it will be really hard and not recomendable to modify the DataGridView. There is an easy workaround though, but you need to post your query on your question so we can go from there. This aproach needs you to automaticaly create columns from the query results; are your columns created automatically (binded)?
Member 10272248 3-Oct-13 17:42pm    
The program will connect to about 2-500 different databases over it's lifetime. These databases have the same user name and password so i'm using Windows Auth.

Everytime the program is loaded the user picks the database and the data is loaded into the gridview from w/e database they pick.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900