Solved

Visual Studio 2008 DataGridView how to change values of a cell

Posted on 2008-10-21
6
1,277 Views
Last Modified: 2013-11-26
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
Comment
Question by:ranhell
  • 3
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
jorgesv13 earned 500 total points
ID: 22772728
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
 

Expert Comment

by:k_balaji004
ID: 22774441
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
 
LVL 10

Author Comment

by:ranhell
ID: 22776855
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: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 8

Expert Comment

by:jorgesv13
ID: 22777011
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
 
LVL 10

Author Comment

by:ranhell
ID: 22777481
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
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22778092
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question