Solved

Visual Studio 2008 DataGridView how to change values of a cell

Posted on 2008-10-21
6
1,282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…

734 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