amber

amber

  • NA
  • 39
  • 48.2k

Code For dataGridView CellClick Event

Jun 15 2013 7:49 AM
Hi,

m trying to fix my code for datagridview cell click event, first i've get set properties in a separate class named as PROPXI, then in another class(fee_endow) i've made a method named getfields(string field) as follows:

public static DataTable getfields(string field)
        {
            DataTable dt = new DataTable();
            using (OleDbConnection conn = new OleDbConnection(Program.constr))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                string ambi1 = "Select * FROM XI WHERE Field=@Field";
                OleDbCommand cmd = new OleDbCommand(ambi1, conn);
                cmd.Parameters.AddWithValue("@Field", field);
                OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
                dap.Fill(dt);
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Dispose();
            }
            return dt;
        }
Then on dataGrid Cell click event:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string field = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            PROPXI pxi = fee_endow.getfields(field);
            textBox4.Text = pxi.SNO.ToString();
            textBox1.Text = pxi.NewName;
            textBox2.Text = pxi.FatherName;
            textBox3.Text = pxi.Profession;
            textBox18.Text = pxi.AcademicRecord.ToString();
            comboBox3.Text = pxi.OwnsCar;
            comboBox1.Text = pxi.ResArea;
            textBox19.Text = pxi.PerReq.ToString();
            comboBox4.Text = pxi.Dependents.ToString();
            comboBox6.Text = pxi.Widow_Divorcee;
            textBox5.Text = pxi.ResSize.ToString();
            comboBox2.Text = pxi.Unit;
            avgtotal.Text = pxi.UtilityBill.ToString();
            totalbox.Text = pxi.Edu_Expenses.ToString();
            inctotal.Text = pxi.MonthlyIncome.ToString();
            radioEarning.Checked = pxi.Earning;
            radioNoearning.Checked = pxi.NonEarning;
            radioOwned.Checked = pxi.Owned;
            radioRented.Checked = pxi.Rented;
            radioHouse.Checked = pxi.House;
            radioFlat.Checked = pxi.Flat;
            if (pxi.Earning)
            {
                radioNoearning.Checked = false;
            }
            else
            {
                radioNoearning.Checked = true;
            }
            if (pxi.Owned)
            {
                radioRented.Checked = false;
            }
            else
            {
                radioRented.Checked = true;
            }
            if (pxi.House)
            {
                radioFlat.Checked = false;
            }
            else
            {
                radioFlat.Checked = true;
            }

        }

but it's giving error:

 Cannot implicitly convert type 'System.Data.DataTable' to 'Endowment.PROPXI'
plz help wts wrong with my method getfields() n code plz highlight,
Thnx




Answers (16)