最佳解答者
Problem on Using Data Grid View on VB.Net

問題
-
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
Chi2008年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 SnippetSystem.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
所有回覆
-
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
Chi2008年5月2日 下午 02:32 -
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.
Thanks2008年5月7日 上午 09:33 -
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 SnippetSystem.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