PDA

View Full Version : سوال: نوشتن عدد در گرید ویو



docendo
جمعه 20 بهمن 1391, 19:55 عصر
در یکی از ستونها فقط بشود عدد نوشت اگر یک حرف به اشتباه تیک شود پیغام دهد که مثلا اشتباه است این ستون متن قبول نمیکند

khokhan
جمعه 20 بهمن 1391, 21:42 عصر
در یکی از ستونها فقط بشود عدد نوشت اگر یک حرف به اشتباه تیک شود پیغام دهد که مثلا اشتباه است این ستون متن قبول نمیکند

ستونی که فقط عدد می پذیرد

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;
}
}
}
}

docendo
جمعه 20 بهمن 1391, 22:44 عصر
ستونی که فقط عدد می پذیرد

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;
}
}
}
}


دستتان درد نکند
کد خوبی بود کار کرد فقط یک ایراد دارد

اگر به سوال من توجه کرده اید نوشته بودم در یکی از ستونها نه همه ستونها
این باعث میشه در همه ستونها فقط عدد بپذیرد
برای نمونه 6 تا ستون داریم که ستون 3و4 عدد قبول کند بقیه بشود تکست هم نوشت

khokhan
جمعه 20 بهمن 1391, 23:41 عصر
اینو امتحان کن

اندیس ستونی رو که می خوای در مقابل == وارد کن



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);
}
}
}

docendo
جمعه 20 بهمن 1391, 23:53 عصر
مرسی
اما این کد برای کار من نیست کلا اشتباهه
در مثلی که نوشته اید وقتی توی سلول 0 کلیک بکنید که عدد نوشته شود کلا از برنامه خارج میشود

khokhan
شنبه 21 بهمن 1391, 00:15 صبح
ببخشین متوجه نبودم

اینو امتحان کن


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;
}
}

docendo
شنبه 21 بهمن 1391, 00:20 صبح
تشکر از لطفتان

این کد را پیدا کردم که کار میکند


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;
}
}
}
}
}

docendo
شنبه 21 بهمن 1391, 00:23 صبح
تفریبا اولین کدی که شما پست کردید میباشد با این تفاوت که میبایست این کد زیر را برای انتخاب ستون اضافه میکردیم

if (e.ColumnIndex == 2)


بهرحال بی نهایت شما تشکر میکنم


این هم لینک کد (http://stackoverflow.com/questions/7615050/validation-on-a-single-datagridview-column)