• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1308
  • Last Modified:

Visual Studio 2008 DataGridView how to change values of a cell

I have a Data Gridview which is bound to a table.
How do I change the visual value in a row depending on the table value.
What I Mean is, for example
table1
FName     LName   Status
John        Smith         1
Mark        Doe            2

And the DataGridview will show the exact values from the table, but I need to change the value of the grid on the Status field to
1 = Active
2 = Inactive

So how do I iterate through the DataGridView and change the values only in the Grid not on the table

thanks in advance for UR help
0
ranhell
Asked:
ranhell
  • 3
  • 2
1 Solution
 
jorgesv13Commented:
You have to use the DataBindingComplete Event of the DataGrid.
Then you can iterate through the columns and change the values.
I suggest you create another unbound column for displaying the Status Text.
Then, you can set to unvisible the "Status" column, and to visible the "StatusText" column.
Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
 
            For Each dr As DataGridViewRow In DataGridView1.Rows
                Select Case dr.Cells("Status").Value
                    Case 1
                        dr.Cells("StatusText").Value = "Active"
                    Case 2
                        dr.Cells("StatusText").Value = "Inactive"
                End Select
            Next
End Sub

Open in new window

0
 
k_balaji004Commented:
In the Grid  DataRowBound event, you can check
if (e.Row.RowType == DataControlRowType.DataRow)
and get the DataBinder.Eval(e.Row.DataItem, "Status").ToString() value.
after that u can compare this value with 1 and 2. based upon the DB value u can assaign the your own text to the Status grid column.
0
 
ranhellAuthor Commented:
Jorgesv13

I did test your code and I go the following concerns/results:
1.-Why should I need to use the ("IDSTATDataGridViewTextBoxColumn") name instead of the shortname the would be "IDSTAT". coz if I use this one I get an error saying columname not found
2.-When trying to change the value to "ACTIVE" or "INACTIVE I get an error, attached you find the error description.
The error is correct, what I mean is on the DB the field IDSTAT is a byte field so by changing the value to ACTIVE or INACTIVE it doesn't support this change.
So does this mena that is trying to change the value on the DB or ....!!

                   Select Case dr.Cells("IDSTATDataGridViewTextBoxColumn").Value
                Case 1
                    dr.Cells("IDSTATDataGridViewTextBoxColumn").Value = "ACTIVO"
                Case 2
                    dr.Cells("IDSTATDataGridViewTextBoxColumn").Value = "Inactive"
            End Select
        Next

Error.GIF
0
[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
jorgesv13Commented:
Ranhell,

1. You need to use the fullname of the column, because it is the identifier for .Net. You can't just use the Header Text. If you want to change the fullname, go to the properties of the column, and then you can change it to something shorter.

2. This is because the column "IDSTATDataGridViewTextBoxColumn" is databound to a byte field, so it takes the same data type.
What you should do is create a DIFFERENT column (like in the code I sent you, the "StatusText" column) for holding the "Active" / "Inactive" string.
This different column should have Visible property set to True, and the column "IDSTATDataGridViewTextBoxColumn" should have the Visible property set to False.

And no, it is not trying to update the DB, however the DataGridView performs validations to ensure that the datatype the user enter is the same as expected.
0
 
ranhellAuthor Commented:
Thanks it worked just fine
I noticed that when you add a datagrid from the toolbox and assigned a bindingsource you get the
fullname like i mentioned before "IDSTATDataGridViewTextBoxColumn" and then if you remove and add the same field again you only get the short field name from the DB wich is "IDSTAT"
any comments on this

I don't wanna be exesive here by asking a realated question
but if you don't mind
Let's say I have these 2 tables

table1                                       table2
FName     LName   Status         Idstat      Desc
John        Smith         1                 1            ACTIVE
Mark        Doe            2                 2            INACTIVE

I tried to add a Query joining these 2 table and showing the Desc field from table2
and I got the "The schema returned by the new query differs from the base query error"
so then I needed a work arround for these which is pretty much what your code did.

Now How do I get the Query to work on the grid, I've read that is better to build the datagrid programmatically instead of dropping it into the form and assigned the query by code.

Could you help me out with these, point are not a problem I will accept your code as the solution, and if needed I will open up another question for these new scenario, but since I have you attention I would like to keep in touch with you with these new concern also

send comments
0
 
jorgesv13Commented:
Actually I don't understand very well what you want to accomplish.
You can add a new query with the joining in the Table Adapter of your Dataset.
It's not a problem that the schema is not the same as the base query (VS will give you a warning, but it will still let you continue)

And, it will be nice if you accept my previous post as the solution :)
I just started on Experts-Exchange 2 days ago, so I want to earn as much points as I can!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now