Solved

Visual Studio 2008 DataGridView how to change values of a cell

Posted on 2008-10-21
6
1,273 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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