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
Loading
Iftikar HussainPosted Jun 25, 2013, 11:50 PM
Can you please provide me the code? so that I will debug it and fix it.
Regards,
Iftikar
VulpesPosted Jun 25, 2013, 2:41 PM
amberPosted Jun 25, 2013, 12:10 PM
Iftikar HussainPosted Jun 25, 2013, 11:25 AM
Regards,
Iftikar
amberPosted Jun 25, 2013, 10:54 AM
Iftikar HussainPosted Jun 20, 2013, 8:10 AM
You first need to change the return type of the method getfields from DataTable to PROPXI. and assign the value as like below
public static PROPXI getfields(int id)
Then it will start work.
Let me know if you need still more information
Regards,
Iftikar
Remember to click "Mark as Answer" on the post, if it helps you.
VulpesPosted Jun 20, 2013, 8:00 AM
amberPosted Jun 20, 2013, 5:52 AM
in class(fee_endow):
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;
}
in another(pswd) class:
public static DataTable getfields(string field)
{
return fee_endow.getfields(field);
}
on search_btn event:
private void button8_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = pswd.getfields(comboBox5.Text);
}
and it's working for record retreival either single or multiple, i js want a code for CELL_CLICK event, so dt after retreival when i click on any record den it should be displayed in text boxes,,
Regards,
VulpesPosted Jun 17, 2013, 7:20 PM
amberPosted Jun 17, 2013, 10:55 AM
string field = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
DataRow dr = fee_endow.getfields(field).Rows[0];
textBox4.Text = dr["SNO"].ToString();
textBox1.Text = dr["NewName"].ToString();
textBox2.Text = dr["FatherName"].ToString();
textBox3.Text = dr["Profession"].ToString();
textBox18.Text = dr["AcademicRecord"].ToString();
comboBox3.Text = dr["OwnsCar"].ToString();
comboBox1.Text = dr["ResArea"].ToString();
textBox19.Text = dr["PerReq"].ToString();
comboBox4.Text = dr["Dependents"].ToString();
comboBox6.Text = dr["Widow_Divorcee"].ToString();
textBox5.Text = dr["ResSize"].ToString();
comboBox2.Text = dr["Unit"].ToString();
avgtotal.Text = dr["UtilityBill"].ToString();
totalbox.Text = dr["Edu_Expenses"].ToString();
inctotal.Text = dr["MonthlyIncome"].ToString();
radioEarning.Checked = Convert.ToBoolean(dr["Earning"]);
radioNoearning.Checked = Convert.ToBoolean(dr["NonEarning"]);
radioOwned.Checked = Convert.ToBoolean(dr["Owned"]);
radioRented.Checked = Convert.ToBoolean(dr["Rented"]);
radioHouse.Checked = Convert.ToBoolean(dr["House"]);
radioFlat.Checked = Convert.ToBoolean(dr["Flat"]);
if (radioEarning.Checked == true)
{
radioNoearning.Checked = false;
}
else..........
now there is an exception error:
there is no row at position 0 :(
amberPosted Jun 17, 2013, 8:42 AM
VulpesPosted Jun 16, 2013, 7:48 PM
If your query contains multiple rows, then it's OK for your getfields method to return a DataTable as you can then bind the DGV directly to that:
dataGridView1.DataSource = fee_endow.getfields().DefaultView;
However, if only want to get a single row, then you'd need to return a PROIPXI object (as per the code in my first post) which you can then assign to your 'pxi' variable.
An alternative way of getting a single row would be to not to bother with the PROPXI class at all but to leave the getfields method to return a DataTable and then extract the data from the first (and only) row:
amberPosted Jun 16, 2013, 2:52 PM
Cannot implicitly convert type 'System.Data.DataTable' to 'Endowment.PROPXI'
on the following highlighted line:
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;
}
}
can u help me fix ds and why error for the method on that line???
VulpesPosted Jun 15, 2013, 6:58 PM
You can then bind the DataTable to the DataGridView with a line such as this:
dataGridView1.DataSource = fee_endow.getfields().DefaultView
amberPosted Jun 15, 2013, 5:18 PM
VulpesPosted Jun 15, 2013, 12:56 PM
If your query is returning (at most) one row from the database table, then I think you'd be better using a DataReader rather than a DataAdapter, populating a PROPXI object's properties with the column values for that row and then returning that. Your CellClick eventhandler should then (assuming there are no other errors) work without further ado.
I'm guessing a bit here as I don't know the types of some of the columns but something like the following. Note that if the query should return more than one row then the last one will be used: