araz_pashazadeh
دوشنبه 28 آذر 1390, 12:07 عصر
با عرض سلام و خسته نباشید خدمت شما دوستان عزیز
من کدی را به شکل زیر نوشتم در رخداد EditingControlShowing دیتا گرید یک TextChanged را فعال می کنم تا با هربار عوض شدن متن داخل سلول دیتا گرید این تابع فراخوانی بشه تا اینجای کار من مشکلی ندارم.
مشکل از اینجا شروع میشه که وقتی اول کاربر این ستون را انتخاب کرد و مبلغ را وارد کرد این تابع برای تمامی ستون ها کار میکنه(صدا زده میشه) و هر ستونی را من انتخاب می کنم این رخداد صدا زده میشه.
private void txtCash_TextChanged(object sender, EventArgs e)
{
try
{
if (dataGridViewBankAccount.CurrentRow.Cells["Cash"].EditedFormattedValue.ToString() != String.Empty)
{
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 0;
DataGridViewTextBoxEditingControl editControl = (DataGridViewTextBoxEditingControl)dataGridViewBan kAccount.EditingControl;
editControl.SelectionStart = editControl.Text.Length;
editControl.SelectionLength = 1;
dataGridViewBankAccount.EditingControl.Text = Decimal.Parse(dataGridViewBankAccount.CurrentRow.C ells["Cash"].EditedFormattedValue.ToString(),
NumberStyles.AllowThousands).ToString("N", nfi);
}
}
catch (FormatException)
{
MessageBox.Show("..........................خـطـا در درج اطـلاعـات........................... \n" + " \n", "خـطـا",
MessageBoxButtons.OK, MessageBoxIcon.Error);
dataGridViewBankAccount.EditingControl.Text = null;
}
}
/************************************************** ************************************************** **********************************/
private void dataGridViewBankAccount_EditingControlShowing(obje ct sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox text = e.Control as TextBox;
if (dataGridViewBankAccount.CurrentCell.ColumnIndex == 3)
{
if (text != null)
{
if (dataGridViewBankAccount.CurrentRow.Cells["Cash"].EditedFormattedValue.ToString() != String.Empty)
dataGridViewBankAccount.EditingControl.Text = dataGridViewBankAccount.CurrentRow.Cells["Cash"].EditedFormattedValue.ToString().Remove(0, 5);
text.TextChanged += new EventHandler(txtCash_TextChanged);
}
}
else
text.TextChanged -= new EventHandler(txtCash_TextChanged);
//رنگی کردن سلول در هنگام ویرایش
e.CellStyle.BackColor = Color.Pink;
}
در واقع این قسمت از کد که رخداد را از بین می بره درست کار نمی کنه
else
text.TextChanged -= new EventHandler(txtCash_TextChanged);
نمیشه کاری کرد فقط برای ستونی که من می خوام کار کنه؟
من کدی را به شکل زیر نوشتم در رخداد EditingControlShowing دیتا گرید یک TextChanged را فعال می کنم تا با هربار عوض شدن متن داخل سلول دیتا گرید این تابع فراخوانی بشه تا اینجای کار من مشکلی ندارم.
مشکل از اینجا شروع میشه که وقتی اول کاربر این ستون را انتخاب کرد و مبلغ را وارد کرد این تابع برای تمامی ستون ها کار میکنه(صدا زده میشه) و هر ستونی را من انتخاب می کنم این رخداد صدا زده میشه.
private void txtCash_TextChanged(object sender, EventArgs e)
{
try
{
if (dataGridViewBankAccount.CurrentRow.Cells["Cash"].EditedFormattedValue.ToString() != String.Empty)
{
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 0;
DataGridViewTextBoxEditingControl editControl = (DataGridViewTextBoxEditingControl)dataGridViewBan kAccount.EditingControl;
editControl.SelectionStart = editControl.Text.Length;
editControl.SelectionLength = 1;
dataGridViewBankAccount.EditingControl.Text = Decimal.Parse(dataGridViewBankAccount.CurrentRow.C ells["Cash"].EditedFormattedValue.ToString(),
NumberStyles.AllowThousands).ToString("N", nfi);
}
}
catch (FormatException)
{
MessageBox.Show("..........................خـطـا در درج اطـلاعـات........................... \n" + " \n", "خـطـا",
MessageBoxButtons.OK, MessageBoxIcon.Error);
dataGridViewBankAccount.EditingControl.Text = null;
}
}
/************************************************** ************************************************** **********************************/
private void dataGridViewBankAccount_EditingControlShowing(obje ct sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox text = e.Control as TextBox;
if (dataGridViewBankAccount.CurrentCell.ColumnIndex == 3)
{
if (text != null)
{
if (dataGridViewBankAccount.CurrentRow.Cells["Cash"].EditedFormattedValue.ToString() != String.Empty)
dataGridViewBankAccount.EditingControl.Text = dataGridViewBankAccount.CurrentRow.Cells["Cash"].EditedFormattedValue.ToString().Remove(0, 5);
text.TextChanged += new EventHandler(txtCash_TextChanged);
}
}
else
text.TextChanged -= new EventHandler(txtCash_TextChanged);
//رنگی کردن سلول در هنگام ویرایش
e.CellStyle.BackColor = Color.Pink;
}
در واقع این قسمت از کد که رخداد را از بین می بره درست کار نمی کنه
else
text.TextChanged -= new EventHandler(txtCash_TextChanged);
نمیشه کاری کرد فقط برای ستونی که من می خوام کار کنه؟