locked
Problem on Using Data Grid View on VB.Net RRS feed

  • 問題

  • Hi

    I am having problem on VB.Net Windows Application. I have a DataGridView, where I would like to use it as a table that allow user to click on any cell then it will read the selected record. It seems to work fine, I can Scroll up and down to look for the record I want. But when I drag the scroll bar to the right and view the rest of the record.

    It display a pop up error message "DataGridView Default Error Dialog" it say the System.ArgumentException: Parameter is not valid. To replace this default dialog please handle the DataError Event.

    I think it is because one of the field has DataType Timestamp.

    Could you give me some help please.

    Many thanks

    Chi
    2008年5月2日 上午 07:52

解答

  • Hello ChiYau,

     

    The timestamp data type is just an incrementing number and does not preserve a date or a time. I don’t think it’s necessary for your end user to see it in Data Grid. Have you consider hiding that column in your GridView control? Setting ColumnMapping property to “Hidden” could prevent GridView rending that column on UI.

     

    Code Snippet

    System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter("Select * from AWBuildVersion", "Data Source=.;Initial Catalog=AdventureWorks;Integrated Security=True");

    DataTable dt = new DataTable();

    sda.Fill(dt);

    dt.Columns[0].ColumnMapping = System.Data.MappingType.Hidden;

    dataGridView1.DataSource = dt;

     

     

    Hope this helps

    Wen Yuan

    2008年5月14日 下午 03:06
  • Hello ChiYau,

     

    As you see, timestamp is an incrementing number type clumn. Most of time, it is involved in solution for Concurrence Conflict Issue. SQL Database increments the value in timestamp column when updating the row. Thereby, we can know if that table has been changed after the last time you retrieves the data.

     

    http://msdn.microsoft.com/en-us/library/aa0416cz.aspx

    [Optimistic Concurrency (ADO.NET)]

     

    Hope this helps

    Best regards,

    Wen Yuan

    2008年5月15日 上午 11:56

所有回覆

  • Sorry, I don't get your meaning, could you try to tell more or even try to get some screen shot?

     

    2008年5月2日 上午 09:00
  • Hi

    Sorry to make you confused. After I experiment my problem, I can reform my question. I have a DataGridView on my VB.Net Application. The DataSource of the DataGridView has a field that contain a timestamp value. and it makes VB returns an error message.

    DataGridView Default Error Dialog" System.ArgumentException: Parameter is not valid... To replace this default dialog please handle the DataError Event.

    I am wondering how could I make timestamp appear on the DataGridView

    Many thanks

    Chi

    2008年5月2日 下午 02:32
  • Do you mean the DataSource has one parameter which is a timestamp?

    And now when you run the application and browse the page, you got error with the date value you entered?

    You may have to make sure your web server, database server and DB are created in same date format.

     

    2008年5月5日 上午 04:28
  • Yeah, DataSource has a parameter that has a type of timestamp.

    I am having a problem on displaying a data from database to DataGridView, whenever the timestamp is displays it will display an error.

    Many thanks

    Chi
    2008年5月6日 上午 07:42
  • Could you show us your code, the DataSource's select command and also the DB table schema.

    2008年5月7日 上午 01:52
  • Hi Ken

    Thanks for your reply

    Here it is my Code on my VB Application

       Private adapter_Records As SqlDataAdapter
        Private cmdBuilder_Records As SqlCommandBuilder
        Private tbl_Records As DataTable
        Private bSource_Records As BindingSource
        Private cmd_Records As SqlCommand

     Private Sub Handle_Records()
            Dim query As String = "SELECT * FROM Items"
            adapter_Records = New SqlDataAdapter(query, ConStr)
            cmdBuilder_Records = New SqlCommandBuilder(adapter_Records)
            tbl_Records = New DataTable
            adapter_Records.Fill(tbl_Records)
            bSource_Records = New BindingSource
            bSource_Records.DataSource = tbl_Records
            dgv_Records.DataSource = bSource_Records
        
        End Sub

    dgv_Records is a DataGridView Control


    The Script below the the schema of the table

    CREATE TABLE [dbo].[Items](
        [Item_id] [int] IDENTITY(1,1) NOT NULL,
        [Description] [varchar](500) NULL,
        [Item] [varchar](50) NULL,
        [DOE] [timestamp] NULL,
        [Price] [money] NULL,
     CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED
    (
        [Item_id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO
    SET ANSI_PADDING OFF

    Hope this help.

    Thanks

    2008年5月7日 上午 09:33
  • in your DB, DOE is "nullable"?

     

    2008年5月8日 上午 03:23
  • yeah it is nullable, because i think it does not really matter, as the timestamp field will ass the current timestamp automatically isn't it?

    Please correct me if my concept is wrong.

    Thanks
    2008年5月8日 上午 07:31
  • No...it won't, you could set the "getDate()" into that column's default value.

     

    2008年5月8日 上午 07:34
  • I tried to modify the DB schema, I do not think I am allowed to set the default value of DOE, which is in DataType TimeStamp. I do not think I can set the default value to GetDate().

    Any Idea?

    2008年5月8日 上午 08:07
  •  

    Oh no...sorry, I misunderstand that it is datetime datatype. Well, seems like timestamp has problem with gridview. Do you think that you can change it from timestamp into datetime, and also update all your sql query to update the column with getdate() or datetime.now() in VB/C#.
    2008年5月8日 上午 10:26
  • Hi

    i cannot change the datatype for that table. I tried to use convert /Cast to datetime, unfortunately it does not work properly. When I use Convert, it displays nothing. When I use Cast it display the datetime value in 1900-01-01 00:05:19.633 ...etc.

    Is there any way I could convert it on the SQL query? The worst case would be not to show this field on the DataGridView. : (

    Thanks for your help.
    2008年5月8日 下午 01:58
  • Hello ChiYau,

     

    The timestamp data type is just an incrementing number and does not preserve a date or a time. I don’t think it’s necessary for your end user to see it in Data Grid. Have you consider hiding that column in your GridView control? Setting ColumnMapping property to “Hidden” could prevent GridView rending that column on UI.

     

    Code Snippet

    System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter("Select * from AWBuildVersion", "Data Source=.;Initial Catalog=AdventureWorks;Integrated Security=True");

    DataTable dt = new DataTable();

    sda.Fill(dt);

    dt.Columns[0].ColumnMapping = System.Data.MappingType.Hidden;

    dataGridView1.DataSource = dt;

     

     

    Hope this helps

    Wen Yuan

    2008年5月14日 下午 03:06
  • Hi Wen Yuan

    Thanks for your information. I mis-lead by that datatype, as it said it is timestamp, so I assure it stamp the date and time there. Would you mind to explain in what situation people will use timestamp? Because the size is smaller than INT?

    Thanks very much for your help.
    2008年5月15日 上午 08:02
  • Hello ChiYau,

     

    As you see, timestamp is an incrementing number type clumn. Most of time, it is involved in solution for Concurrence Conflict Issue. SQL Database increments the value in timestamp column when updating the row. Thereby, we can know if that table has been changed after the last time you retrieves the data.

     

    http://msdn.microsoft.com/en-us/library/aa0416cz.aspx

    [Optimistic Concurrency (ADO.NET)]

     

    Hope this helps

    Best regards,

    Wen Yuan

    2008年5月15日 上午 11:56