در یکی از ستونها فقط بشود عدد نوشت اگر یک حرف به اشتباه تیک شود پیغام دهد که مثلا اشتباه است این ستون متن قبول نمیکند
در یکی از ستونها فقط بشود عدد نوشت اگر یک حرف به اشتباه تیک شود پیغام دهد که مثلا اشتباه است این ستون متن قبول نمیکند
ستونی که فقط عدد می پذیرد
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewTextBoxCell cell = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell;
if (cell != null)
{
char[] chars = e.FormattedValue.ToString().ToCharArray();
foreach (char c in chars)
{
if (char.IsDigit(c) == false)
{
MessageBox.Show("You have to enter digits only");
e.Cancel = true;
break;
}
}
}
}
اینو امتحان کن
اندیس ستونی رو که می خوای در مقابل == وارد کن
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
TextBox tb = e.Control as TextBox;
tb.KeyPress -= new KeyPressEventHandler(tb_KeyPress);
if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
{
tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
}
}
}
مرسی
اما این کد برای کار من نیست کلا اشتباهه
در مثلی که نوشته اید وقتی توی سلول 0 کلیک بکنید که عدد نوشته شود کلا از برنامه خارج میشود
ببخشین متوجه نبودم
اینو امتحان کن
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
}
}
}
private void Column1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
تشکر از لطفتان
این کد را پیدا کردم که کار میکند
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewTextBoxCell cell = dataGridView1[2, e.RowIndex] as DataGridViewTextBoxCell;
if (cell != null)
{
if (e.ColumnIndex == 2)
{
char[] chars = e.FormattedValue.ToString().ToCharArray();
foreach (char c in chars)
{
if (char.IsDigit(c) == false)
{
MessageBox.Show("You have to enter digits only");
e.Cancel = true;
break;
}
}
}
}
}
تفریبا اولین کدی که شما پست کردید میباشد با این تفاوت که میبایست این کد زیر را برای انتخاب ستون اضافه میکردیم
if (e.ColumnIndex == 2)
بهرحال بی نهایت شما تشکر میکنم
این هم لینک کد