[英]C# Datagridview , first column (ID) must be primary key without using database, how to setup it?
我有一個小問題,不知道如何設置第一列(ID)的主鍵? 有人可以幫忙嗎?
這是我的代碼:
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;
}
如果你想這樣使用 GIUD 作為字符串:
string guid = System.Guid.NewGuid().ToString()
即使認為擁有重復 GIUD 的可能性可以忽略不計,但在你的情況下,你有一個“比喻”的 ID,這意味着你不知道你是否曾經有一個重復的 ID,我應該提到使用 giud 作為一個 ID而不是數字是浪費 memory 在這些情況下效率低下,但我不知道你的意思是什么。
PS:如果您只想要一個數字,請使用隨機數生成器,但獲得重復 ID 的可能性將與您指定的數字范圍成反比:
Random r = new Random();
r.Next(start_range, end_range);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.