Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Note it is windows application.

Run mode screen as follows;

Course Code Combo box

Datagridview as follows;

Days sess1 sess 2 sess3 sess4
1 checkbox checkbox checkbox checkbox

In Database record as follows;

course Code Days sess1 sess2 sess3 sess4
REO 1 checkboxchecked checkboxchecked checkboxchecked checkboxchecked


Then i have one button in the run mode called Search Button when click the REO Course in the combo box, in the datagridview that particular course details to be displayed in the datagridview.


For that search button code as follows;
C#
int irow;
sql = "select [Days],[sess1],[sess2],[sess3],[sess14] from Tb_Session_Structure where Cmn_Minor_code = '" + cb_Course_Code.Text.ToString() + "'";

dr = GFun.ReadAcessSql(sql);
while (dr.Read())
{
    for (irow = 0; irow < DGv_Session.RowCount; irow++)
    {
        DGv_Session.Rows[irow].Cells[0]  = dr[0].ToString().Trim();
        DGv_Session.Rows[irow].Cells[1] = dr[1].ToString().Trim();
        DGv_Session.Rows[irow].Cells[2] = dr[2].ToString().Trim();
        DGv_Session.Rows[irow].Cells[3] = dr[3].ToString().Trim();
        dr.Close();
    }
}


when i click the Search button error shows as follows;

Cannot implicitly convert type string to System.Windows.Forms.DataGridViewCell

what is the problem in my above code.
in the search button what is the mistake i made.

please help me.

Regards,

Narasiman P.
Posted
Updated 15-Mar-13 2:41am
v2
Comments
Jegan Thiyagesan 15-Mar-13 8:42am    
you have been going on about these questions for a week, Is this a piece of homework you are doing at the last minute by any chance?

write
<br />
DGv_Session.Rows[irow].Cells[0].Value  = dr[0].ToString().Trim();



this will work fine
 
Share this answer
 
Hi,
I don't mind helping you with your homework, but you should understanad the concepts behind it.

Say your a human, and some one giving you food in spoon, which one of the below would you think is better,

1) dropping the food in the spoon on your head?
or
2)giving the food in the spoon in your mouth?

So dropping the food on you head is not going to be useful to you, is it?

so the food should be given in the mouth.

if DGv_Session.Rows[irow].Cells[0] is the head, what would be the mouth?

Here is a clue, something called "Value".

Regards
Jegan
 
Share this answer
 
v3
The clue is in the error message
Quote:
Cannot implicitly convert type string to System.Windows.Forms.DataGridViewCell

Using debugging you should have determined that it is this line that is throwing the exception
DGv_Session.Rows[irow].Cells[0]  = dr[0].ToString().Trim();

Funnily enough because DGv_Session.Rows[irow].Cells[0] is a DataGridViewCell and
dr[0].ToString().Trim() is a string.
You need to set the Value of the cell to the returned value from the database, not the cell itself.

Take care how you do this as I believe these are checkboxes you are trying to set ... and you can't set a CheckBox value to a string either.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900