PDA

View Full Version : سوال: بازگشت اطلاعات از گریدویو در تکست باکس برای ویرایش(Linq)



ashkan1234
چهارشنبه 04 بهمن 1391, 21:29 عصر
با سلام!من از کد زیر برای بازگشت اطلاعات در تکست باکسها اسفاده میکنم!
ولی وقتی رو هرکدوم از هدرهای گریدویو کلیک میکنم با خظا مواجه میشم!

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
try
{
txtname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
txtfamily.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
txttel.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
catch (Exception)
{

throw;
}



}
به throw گیرمیده!میگه شئ نمونه گیری نشده!
Object reference not set to an instance of an object.
ممنون میشم راهنمایی بفرمایید!

Y_Safaiee
پنج شنبه 05 بهمن 1391, 00:53 صبح
با سلام!من از کد زیر برای بازگشت اطلاعات در تکست باکسها اسفاده میکنم!
ولی وقتی رو هرکدوم از هدرهای گریدویو کلیک میکنم با خظا مواجه میشم!

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
try
{
txtname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
txtfamily.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
txttel.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
catch (Exception)
{

throw;
}



}
به throw گیرمیده!میگه شئ نمونه گیری نشده!
Object reference not set to an instance of an object.
ممنون میشم راهنمایی بفرمایید!

سلام دوست خوبم
بهتره جای SelectionChanged از CellClick استفاده کنین اینطوری مدیریت بیشتری میشه رو رفتار گرید ویو انجام داد.

طبق کد زیر :



private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 || e.ColumnIndex!=-1)
{
textBox1.Text= dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
}
}



موفق باشی
بایت بایت