是DataGridView的任何属性,除了唯一值之外,只有唯一值表示唯一RollNo [英] Is their any property of the DataGridView which excepts only unique values say unique RollNo
本文介绍了是DataGridView的任何属性,除了唯一值之外,只有唯一值表示唯一RollNo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是dataGridView的它们的任何属性,除了唯一值之外,在Windows窗体中,唯一的值表示ROLLNUMBER列的唯一RollNo和值的行.
Is their any property of the dataGridView which excepts only unique values say unique RollNo for the column ROLLNUMBER and the values row wise in windows form.
推荐答案
在datagridview的数据源(即数据表)上执行此操作.在添加数据表之前,将数据列的Unique属性设置为true,请参见示例:
You can do it at the datasource of the datagridview, that is the datatable. Set the Unique property of the datacolumn to true before adding the datatable, see example:
DataTable dataTable = new DataTable();
DataColumn uniqueColumn = new DataColumn("Unique ID");
uniqueColumn.DataType = System.Type.GetType("System.Int32");
uniqueColumn.Unique = true;
dataTable.Columns.Add(uniqueColumn);
dataTable.Columns.Add("name", typeof(string));
try
{
dataTable.Rows.Add(1, "christ");
dataTable.Rows.Add(2, "william");
dataTable.Rows.Add(3, "albert");
dataTable.Rows.Add(1, "martin");
dataTable.Rows.Add(2, "allen");
dataTable.Rows.Add(3, "moorthy");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
不,没有.
DataGridView(DGV)是用于显示数据的图形用户界面(GUI)的元素.如果您想提供上述功能,则需要扩展DGV属性.
No, there isn''t.
DataGridView (DGV) is an element of graphical user interface (GUI) to display data. If you would like to provide above functionality, you need to extend DGV properties.
没有此类属性,您必须自己制作,就像这样(未测试):
There is no such property, you have to make your own, like this(not tested):
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)// the column you want to have unique input
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
TextBox txt = sender as TextBox;
string cleanInput = txt.Text;
int maxChar = 5; // Here the maximum number of characters
if (cleanInput.Length > 0)
{
char last = cleanInput[cleanInput.Length - 1];
int tot = cleanInput.Length;
if (tot != 5 && last != ''S'') // example for allowing characters
{
txt.Text = CleanInput(cleanInput);
}
else return;
}
private static string CleanInput(string strIn)
{
return Regex.Replace(strIn, @"[^0-9]+", "");// example for only numbers.
}
这篇关于是DataGridView的任何属性,除了唯一值之外,只有唯一值表示唯一RollNo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文