PDA

View Full Version : حرفه ای: مشکل در EditingControlShowing دیتا گرید



araz_pashazadeh
دوشنبه 28 آذر 1390, 11: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);
نمیشه کاری کرد فقط برای ستونی که من می خوام کار کنه؟

reza_mostafavi63
دوشنبه 28 آذر 1390, 11:47 صبح
چرا اين كار رو نميكني :

//
private void txtCash_TextChanged(object sender, EventArgs e)
{
if (dataGridViewBankAccount.CurrentCell.ColumnIndex == 3)
{
//....


}
}

araz_pashazadeh
دوشنبه 28 آذر 1390, 16:40 عصر
چرا اين كار رو نميكني :

//
private void txtCash_TextChanged(object sender, EventArgs e)
{
if (dataGridViewBankAccount.CurrentCell.ColumnIndex == 3)
{
//....


}
}

دوست عزیز ممنون از راهنمایی شما.
این روشی که شما پیشنهاد کردین فقط کاری میکنه که کدهای من اجرا نشه.
من می خواستم بدونم منطق این کدی که من نوشتم اینه که برای همه ستونها کار کنه؟
یا می تونم کاری کنم که فقط برای یک ستون خاص کار کنه؟
البته من این تکه کد را نوشتم اجرا هم میشه ولی باز این تابع فراخوانی میشه.
else
text.TextChanged -= new EventHandler(txtCash_TextChanged);
ولی به نظر من باید کد بالا کار کنه چون دلیقهای که نوشته میشن به همین صورت از حالت اجرا در می ایند یا به حالت اجرا فرستاده می شوند.