Solved

Visual Studio 2008 DataGridView how to change values of a cell

Posted on 2008-10-21
6
1,261 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Expert Comment

by:jorgesv13
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now