5 Replies - 25788 Views - Last Post: 08 October 2014 - 06:46 AM Rate Topic: -----

#1 foxbymariuss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-May 09

DataGridView change column type programatically

Posted 02 July 2009 - 12:48 AM

Hello,

I have the following problem regarding DataGridView. I am kinda newbie to VB, so I apologize if the question is a stupid one.

So:
I am using VB 2008.
I have a DataGridView that I use for the output of a SQL select statement. The application composes the SQL select statement, runs it against the database, puts the result in a dataadapter that fills a datatable. The datatable is then set as Datasource for the Datagridview. The DGV column headers are the aliases from the SQL select statement.
The select Statement returns a column that is of datatype Date. The user needs to edit that column. It's much easier to do this via a calendar control (or date - time picker). So using the example found at http://msdn.microsof...s/7tas5c80.aspx
I managed to create a column of type date that can be edited by selecting a date from a calendar control.
What I need and don't know how to do is how to set a certain column (that is of date datatype) to be of a certain type (date picker combo box) after binding the datatable to the DGV(after setting the datasource for DGV as being the datatable).
Can anyone please help me with this?

Thank you for any suggestions.

Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView change column type programatically

#2 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: DataGridView change column type programatically

Posted 02 July 2009 - 04:18 AM

Can we see the code that you are working on please? :)
Was This Post Helpful? 0
  • +
  • -

#3 SpeeDemon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 18-March 08

Re: DataGridView change column type programatically

Posted 02 July 2009 - 11:58 AM

I too am interested in seeing some code as I am working on a similar situation (Also a noobie) :)
Was This Post Helpful? 0
  • +
  • -

#4 foxbymariuss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-May 09

Re: DataGridView change column type programatically

Posted 06 July 2009 - 05:52 AM

Yes, of curse. Here it is:



Dim orclstr As String = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.9)(PORT=1523)))(CONNECT_DATA=(SID=orcl)(SERVER=DEDICATED)));User Id=user;Password=pass;"
			Dim strsql As String = "select transportercode as ""Airline"", flightnr as ""Flight Nr."", nvl2(nullif(is_nil,0),'Yes','No') as ""Is NIL"", flight_date as ""Date"", depart_airport as ""Departure"", unloading_airp as ""Destination"", nvl(truck,'N/A') as ""truck"", nvl(aircraft_code, 'N/A') as ""Aircraft Code"", obs as ""Observations"" from ffm where flightnr='" + ListBox1.SelectedValue + "' and flight_date=to_date('" + CStr(DateTimePicker1.Value.Day) + "-" + CStr(DateTimePicker1.Value.Month) + "-" + CStr(DateTimePicker1.Value.Year) + "','dd-mm-yyyy')"
dim dt as datatable = new datatable

'flight_date is a calendar date data type

Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strsql, orclstr)

da = New OleDb.OleDbDataAdapter(strsql, orclstr)

Me.DataGridView2.DataSource = dt





I want the user to be able to edit the field "flight_date". As the app runs now, the data is presented in the cells of this columns like: 30/04/2009 .
The user can edit it, but unless he enters a valid date, in the format dd/mm/yyyy , an error is raised.
For limiting user bad input, I want the user to be able to pick a calendar date from a picker.


To create a column with datetimepicker, I used the code found at: http://msdn.microsof...s/7tas5c80.aspx[/url]


With this I can create a column with date picker, but only before the binding:
Me.DataGridView2.DataSource = dt



I want the column type of flight_date column to be date picker.
How can I do that? How to change the column type after binding? Or, how to declare the DGV column type and bind it after this?

If you have any other suggestions, I would be grateful.

Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#5 taps k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-June 10

Re: DataGridView change column type programatically

Posted 15 June 2010 - 10:04 PM

Hello foxbymariuss
i am newbi in .net framework
you get any solution for your problem i face same problem if ypu have solution please tell me
thanks in advance

i have datagrid in program containing various columns in that one column contains datatime in double datatype format and i am unble to change that runtime into datatime datatype any one can suggest me for this problem
thanks in advance

This post has been edited by andrewsw: 08 October 2014 - 07:23 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 Landrum  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 27-August 14

Re: DataGridView change column type programatically

Posted 08 October 2014 - 06:46 AM

I like this post, it is like a time capsule. I realize it has been five years but I just ran into this same problem. I looked high and low on the net and couldn't find a solution. Here is what I did to solve it; hopefully I won't be crucified as a sloppy programmer, but this was my approach, implementing what little I know.
Under the form class I declared these two variables, right away, so that they can be accessed by my eventhandlers
public class Myform
    Dim This_Cell As DataGridViewCell
    Dim THIS_DataGridView As DataGridView



my datagridview was created at runtime so I then added a handler to it like so, but if yours was created at design time just add the cellmouseclick event.

 AddHandler PropertyListLayout.CellMouseClick, AddressOf Datgridview_Click_Event



I then added the clickevent handler to make a datetimepicker appear directly over the datagridview cell and disabled the datagridview until the user selects a date.

  Public Sub Datgridview_Click_Event(sender As Object, e As DataGridViewCellMouseEventArgs)

        'your column index of the control you wish to control with the datetimepicker
        If e.ColumnIndex = 3 Then
            'cast these now so they can be passed to the next handler
            THIS_DataGridView = CType(sender, DataGridView)
            this_cell = CType(THIS_DataGridView.Item(3, e.RowIndex), DataGridViewCell)


            Dim Calender_control As New DateTimePicker


            THIS_DataGridView.Parent.Controls.Add(Calender_control)
            With Calender_control
                .Format = DateTimePickerFormat.Short
                .Visible = True
                'kind of important I noticed 
                .BringToFront()
                .Padding = New System.Windows.Forms.Padding(0)
                'get the width of the current cell to size the datetimepicker
                .Width = This_Cell.Size.Width
             'this passes the value if there is already a date in the cell, this makes editing easier
                If This_Cell.Value <> "" Then
                    .Value = This_Cell.Value
                End If
                
                'Take into account the datagridview position on the form and the location of the cell on the datgridview
                'if you have the datagridview in a panel or groupbox I think you will have to account for that as well but I haven't tried it
'this is really the hardest part trying to match up the location with the cell
                .Location = THIS_DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location + THIS_DataGridView.Location

               
            End With
            THIS_DataGridView.Enabled = False
'this is now the handlers for the datetimepicker
'this first one is for when the user uses the drop down calendar to select the date
            AddHandler Calender_control.CloseUp, AddressOf Calender_control_value_changed
'this one is for if the user selects another object on the form or clicks off the datetimepicker
            AddHandler Calender_control.LostFocus, AddressOf Calender_control_value_changed
'this one is for if the user hits the enter key after typing in the date themself
            AddHandler Calender_control.KeyDown, AddressOf Calender_control_enterkey

        End If
    End Sub



Then here is the easy part, I passed the value of my datetimepicker to the datagridview
    Private Sub Calender_control_value_changed(sender As Object, e As EventArgs)
        Dim THIS_datetimepicker = CType(sender, DateTimePicker)
'when I created my column I set it to readonly, if you don't do that the user can move into the cell without using the mouse and enter bad data that will throw an exception when the datetimepicker is called
        This_Cell.ReadOnly = False
        This_Cell.Value = THIS_datetimepicker.Value.ToShortDateString
        This_Cell.ReadOnly = true
        THIS_datetimepicker.Dispose()
'don't forget to make the datagridview enabled again
        THIS_DataGridView.Enabled = True
    End Sub



this next handler decides if the enter key is pressed or not. The only way to enter data really is to use the dropdown or press the enter key
    Private Sub Calender_control_enterkey(sender As Object, e As KeyEventArgs)
'if the enter or return key is pressed do the code
        If e.KeyCode = 13 Then

            Dim THIS_datetimepicker = CType(sender, DateTimePicker)
            This_Cell.ReadOnly = False
            This_Cell.Value = THIS_datetimepicker.Value.ToShortDateString
            This_Cell.ReadOnly = true
            THIS_datetimepicker.Dispose()
            THIS_DataGridView.Enabled = True
        End If
    End Sub



I really do hope this is a viable solution if anyone comes across this again. I really want to help with code rather than hinder someone with bad ideas. But this worked for me so I thought it might help others. Cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1