简体   繁体   English

C# Datagridview,第一列(ID)必须是主键,不使用数据库,如何设置?

[英]C# Datagridview , first column (ID) must be primary key without using database, how to setup it?

I have a small problem, don't know how to setup primary key for first column (ID)?我有一个小问题,不知道如何设置第一列(ID)的主键? Can somebody help?有人可以帮忙吗?

This is my code:这是我的代码:

Datatable tabela;

private void Admin_Load(object sender, EventArgs e)
{
    tabela = new DataTable();
    //this column should be primary key
    tabela.Columns.Add("ID", typeof(int));
    tabela.Columns.Add("Naziv", typeof(string));
    tabela.Columns.Add("Zanr", typeof(string));
    tabela.Columns.Add("Duzina", typeof(int));
    tabela.Columns.Add("Granica godina: ", typeof(int));
    dataGridView1.DataSource = tabela;
}

Use GIUD as a string if you want like this:如果你想这样使用 GIUD 作为字符串:

string guid = System.Guid.NewGuid().ToString()

Even thought the possibility of having a duplicate GIUD is negligible, but in your case, you are having a "figurative" Id which means that you would not know IF you ever had a duplicate Id, I should mention that using a giud for an id instead of a number is a wast of memory and it's inefficient in these cases, but I don't know what's your point.即使认为拥有重复 GIUD 的可能性可以忽略不计,但在你的情况下,你有一个“比喻”的 ID,这意味着你不知道你是否曾经有一个重复的 ID,我应该提到使用 giud 作为一个 ID而不是数字是浪费 memory 在这些情况下效率低下,但我不知道你的意思是什么。

PS: if you want a number and nothing else, use a random numbers generatore, but the possibility to get a duplicate ID will increase inversely with respect to the range of numbers you specify: PS:如果您只想要一个数字,请使用随机数生成器,但获得重复 ID 的可能性将与您指定的数字范围成反比:

Random r = new Random();
r.Next(start_range, end_range);

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

相关问题 如何使用C#控制Datagridview中的主键 - How to control primary key in datagridview with c# 如何在c#中获取DataGridView中的主键? - how to get the primary key in a DataGridView in c#? 从数据库使用EntityFramework C#代码优先-如何在没有主键的情况下映射表 - Using EntityFramework C# code-first from database - how to map table with no primary key 如何在C#Winforms中不使用数据库的情况下在列表上使用DatagridView - How to use DatagridView on a List without using Database in C# Winforms 如何使用没有主键的数据库使用DataSet(C#和MySQL)进行更新 - How to update using DataSet (C# and MySQL) with the database with no primary key 如何获取插入数据库中的序列/触发器主键并将其插入到我的类参数ID C#中 - How to get the sequence/trigger primary key that is inserted into the database and insert it into my class parameter id C# 如何使用C#将datagridview更新到数据库? - how to update datagridview to database using c#? C#WinForms-DataGridView / SQL Compact-主键列中的负整数 - C# WinForms - DataGridView/SQL Compact - Negative integer in primary key column MySQL的整个数据库到JSON使用C#,没有datagridview - mysql whole database to json using c# without datagridview 如何使用C#将新插入的行的ID(主键)存储到变量中 - How to store Id (primary key) of newly inserted row into a variable using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM