简体   繁体   English

绑定的数据网格视图中的主键显示负数

[英]Primary Key in bound datagridview displayes negative numbers

I have a datagridview which is bound to table in sqlserver 2012 database. 我有一个datagridview绑定到sqlserver 2012数据库中的表。 All fields are displayed correctly expect the primary key fields which is displayed as a negative numbers. 所有字段都正确显示,期望主键字段显示为负数。 在此处输入图片说明

What is causing this? 是什么原因造成的?

<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
    Private Sub InitCommandCollection()
        Me._commandCollection = New Global.System.Data.SqlClient.SqlCommand(1) {}
        Me._commandCollection(0) = New Global.System.Data.SqlClient.SqlCommand()
        Me._commandCollection(0).Connection = Me.Connection
        Me._commandCollection(0).CommandText = "SELECT TSId, TCId, ElementNo, MSECId, UseInd, ElementDepth, ElementName, ChildInd"& _ 
            ", ElementValueDef, ElementValueNew, ElementPath, ElementPathFull, ElementValuePr"& _ 
            "efix, ElementValueLength, ElementValueIncrement, UpperControlElement1, UpperCont"& _ 
            "rolElementValue1, UpperControlElement2, UpperControlElementValue2, LowerControlE"& _ 
            "lement1, LowerControlElementValue1, LowerControlElement2, LowerControlElementVal"& _ 
            "ue2, AttributeName1, AttributeValue1, AttributeName2, AttributeValue2, Attribute"& _ 
            "Name3, AttributeValue3, AttributeName4, AttributeValue4, AttributeName5, Attribu"& _ 
            "teValue5 FROM dbo.XML_TEST_SET"
        Me._commandCollection(0).CommandType = Global.System.Data.CommandType.Text
        Me._commandCollection(1) = New Global.System.Data.SqlClient.SqlCommand()
        Me._commandCollection(1).Connection = Me.Connection
        Me._commandCollection(1).CommandText = "dbo.SP_XML_TEST_SET_FillByComboBoxes"
        Me._commandCollection(1).CommandType = Global.System.Data.CommandType.StoredProcedure
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@RETURN_VALUE", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.ReturnValue, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyENVId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyUId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyMTId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTestSetName", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTestCaseName", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyInterchangeSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyInstructionSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTransactionSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyElementValueDef", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyElementPathFull", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyFetchRows", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
    End Sub

Stored Proc: 存储过程:

    USE [UToolDb]

GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[SP_XML_TEST_SET_FillByComboBoxes] -- Add the parameters for the stored procedure here @MyENVId AS INT, @MyUId AS INT, @MyMTId AS INT, @MyTestSetName AS NVARCHAR(MAX), @MyTestCaseName AS NVARCHAR(MAX), @MyInterchangeSeqNo AS INT, @MyInstructionSeqNo AS INT, @MyTransactionSeqNo AS INT, @MyElementValueDef AS NVARCHAR(MAX), @MyElementPathFull AS NVARCHAR(MAX), @MyFetchRows AS INT [SP_XML_TEST_SET_FillByComboBoxes]-在此处添加存储过程的参数@MyENVId AS INT,@ MyUId AS INT,@ MyMTId AS INT,@ MyTestSetName AS NVARCHAR(MAX) ,@MyTestCaseName AS NVARCHAR(MAX),@MyInterchangeSeqNo AS INT,@MyInstructionSeqNo AS INT,@MyTransactionSeqNo AS INT,@MyElementValueDef AS NVARCHAR(MAX),@MyElementPathFull AS NVARCHAR(MAX),@MyFetchRows AS INT

AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. AS BEGIN-添加SET NOCOUNT ON以防止多余的结果集干扰SELECT语句。 SET NOCOUNT ON; 开启NOCOUNT;

SET ROWCOUNT @MyFetchRows

DECLARE @sql NVARCHAR(MAX) = N'
SELECT     
TS.TCId,
TS.ElementNo,
TS.MSECId,
TS.UseInd,
TS.ElementDepth,
TS.ElementName,
TS.ChildInd,
TS.ElementValueDef,
TS.ElementValueNew,
TS.ElementPath,
TS.ElementPathFull,
TS.ElementValuePrefix,
TS.ElementValueLength,
TS.ElementValueIncrement,
TS.UpperControlElement1,
TS.UpperControlElementValue1,
TS.UpperControlElement2,
TS.UpperControlElementValue2,
TS.LowerControlElement1,
TS.LowerControlElementValue1,
TS.LowerControlElement2,
TS.LowerControlElementValue2,
TS.AttributeName1,
TS.AttributeValue1,
TS.AttributeName2,
TS.AttributeValue2,
TS.AttributeName3,
TS.AttributeValue3,
TS.AttributeName4,
TS.AttributeValue4,
TS.AttributeName5,
TS.AttributeValue5
FROM dbo.XML_TEST_SET TS 
LEFT JOIN dbo.XML_TEST_SET_OVERVIEW TSO 
ON TSO.TCId = TS.TCId '     
+ CASE WHEN (@MyENVId IS NOT NULL) THEN N'WHERE TSO.ENVId = @MyENVId ' ELSE 'WHERE TSO.ENVId IS NOT NULL '  END
+ CASE WHEN (@MyUId IS NOT NULL) THEN N'AND TSO.UId = @MyUId ' ELSE '' END
+ CASE WHEN (@MyMTId IS NOT NULL) THEN N'AND TSO.MTId = @MyMTId ' ELSE '' END
+ CASE WHEN (@MyTestSetName IS NOT NULL) THEN N'AND TSO.TestSetName = @MyTestSetName ' ELSE '' END 
+ CASE WHEN (@MyTestCaseName IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyInterchangeSeqNo IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyInstructionSeqNo IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyTransactionSeqNo IS NOT NULL) THEN N'AND TSO.TransactionSeqNo = @MyTransactionSeqNo ' ELSE '' END
+ CASE WHEN (@MyElementValueDef IS NOT NULL) THEN N'AND TS.ElementValueDef = @MyElementValueDef ' ELSE '' END 
+ CASE WHEN (@MyElementPathFull IS NOT NULL) THEN N'AND TS.ElementPathFull = @MyElementPathFull ' ELSE '' END
+ N'ORDER BY TSO.TestSetName, TSO.TestCaseName, TS.ElementNo, TSO.InterchangeSeqNo, TSO.InstructionSeqNo, TSO.TransactionSeqNo;';

EXEC sp_executesql @sql, N' @MyENVId INT, @MyUId INT, @MyMTId INT, @MyTestSetName NVARCHAR(MAX), @MyTestCaseName NVARCHAR(MAX), @MyInterchangeSeqNo SMALLINT, @MyInstructionSeqNo SMALLINT, @MyTransactionSeqNo SMALLINT, @MyElementValueDef NVARCHAR(MAX), @MyElementPathFull NVARCHAR(MAX)', @MyENVId, @MyUId, @MyMTId, @MyTestSetName, @MyTestCaseName, @MyInterchangeSeqNo, @MyInstructionSeqNo, @MyTransactioNSeqNo, @MyElementValueDef, @MyElementPathFull; EXEC sp_executesql @sql,N'@MyENVId INT,@MyUId INT,@MyMTId INT,@MyTestSetName NVARCHAR(MAX),@MyTestCaseName NVARCHAR(MAX),@MyInterchangeSeqNo SMALLINT,@MyInstructionSeqNo SMALLINT,@MyTransactionSeqNo SMALLINT,@MyElementValueDef ),@ MyElementPathFull NVARCHAR(MAX),@ MyENVId,@ MyUId,@ MyMTId,@ MyTestSetName,@ MyTestCaseName,@ MyInterchangeSeqNo,@ MyInstructionSeqNo,@ MyTransactioNSeqNo,@ MyElementValueDef,@ MyElementPathFull; END 结束

If you used the wizard to generate your dataset, It will automatically set the auto increment to -1, because it has no way of knowing what data is in the database it has no idea what number to use as an auto-increment, in which case -1 would be the best solution. 如果您使用向导生成数据集,它将自动将自动增量设置为-1,因为它无法知道数据库中有哪些数据,因此不知道将哪个数字用作自动增量。情况-1是最好的解决方案。

check out here social.msdn.microsoft.com 在这里查看social.msdn.microsoft.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM