[英]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.