Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have MVP project I am trying to display the selected row to 3 textboxs and 1 checkbox :

I know I can do it from the CellClick event like so:
C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int index = e.RowIndex;// get the Row Index
    DataGridViewRow selectedRow = dataGridView1.Rows[index];
    textBox1.Text = selectedRow.Cells[0].Value.ToString();
    textBox2.Text = selectedRow.Cells[1].Value.ToString();
    textBox3.Text = selectedRow.Cells[2].Value.ToString();
    textBox4.Text = selectedRow.Cells[3].Value.ToString();
    checkBox1.Checked = Convert.ToBoolean(selectedRow.Cells[4].Value);
}

But I want to call it from other class called Presenter but I am getting errors:

1. The name 'e' does not exist in the current context .

2. 'object' does not contain a definition for 'Rows' and no accessible extension method 'Rows' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?).

3. Cannot implicitly convert type 'string' to 'int'.

Thanks...

What I have tried:

I have Model Which Contain the following code:

C#
namespace TestMVP.Models
{
    class OpsModel
    {
        public int CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string CustomerEmail { get; set; }
        public string CustomerPhone { get; set; }
        public bool CustomerStatus { get; set; }
        public byte CustomerPhoto { get; set; }
        
    }
}


And I have Inerfaces Which Contain :

C#
namespace TestMVP.Views.Inerfaces
{
    public interface IOps
    {
         int CustomerID { get; set; }
         string CustomerName { get; set; }
         string CustomerEmail { get; set; }
        string CustomerPhone { get; set; }
         bool CustomerStatus { get; set; }
         object DataGridView { get; set; }
         object simpleButton2 { get; set; }
         object simpleButton3 { get; set; }
         object simpleButton4 { get; set; }

        //byte CustomerPhoto { get; set; }
    }
}



Now from Servicers Class i have this code to get all Customers from database


C#
static public DataTable GetAllCustomer()
        {
            return DBHelper.GetData("Get_All_Customer", () => { });
        }



in presenter Class i have this code which have the errors :


C#
public void GetDataToTextBox(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;// get the Row Index
            DataGridViewRow selectedRow = iops.DataGridView.Rows[index];
            iops.CustomerID = selectedRow.Cells[0].Value.ToString();
            iops.CustomerName = selectedRow.Cells[1].Value.ToString();
            iops.CustomerEmail = selectedRow.Cells[2].Value.ToString();
            iops.CustomerPhone = selectedRow.Cells[3].Value.ToString();
            iops.CustomerStatus = selectedRow.Cells[4].Value.ToString();
        }


the errors are from the above code in presenter
Posted
Updated 10-Dec-20 7:08am
v3
Comments
Richard Deeming 10-Dec-20 8:58am    
If you want someone to help you fix the code that doesn't work, you need to show us the code that doesn't work.

Click the green "Improve question" link and replace the code in the "What I have tried" section with the code that generates the three errors you're trying to fix.
khalid4775 10-Dec-20 11:35am    
Updated The Question :)

Taking the errors one at a time:

The name 'e' does not exist in the current context.
Would not happen in the GetDataToTextBox method shown, since e is a parameter to the method.

'object' does not contain a definition for 'Rows'
The iops.DataGridView property is declared as returning an object, not a DataGridView. Fix it to return the correct type.

Cannot implicitly convert type 'string' to 'int'.
The CustomerID property is declared as an int. You are trying to set it to a string:
C#
iops.CustomerID /*int*/ = selectedRow.Cells[0].Value.ToString() /*string*/;
You'll have a similar error with the CustomerStatus property - it is declared as bool, and you are trying to set it to a string.
 
Share this answer
 
Comments
khalid4775 10-Dec-20 12:06pm    
Iam New in MVP can you modify it for me ?
Thanks
Thanks For Help Errors removed after fixing the code to be like so :

C#
public void GetDataToTextBox(DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;// get the Row Index
            DataGridViewRow selectedRow = iops.DataGridViewRows.Rows[index];
            iops.CustomerID = Convert.ToInt32( selectedRow.Cells[0].Value);
            iops.CustomerName = selectedRow.Cells[1].Value.ToString();
            iops.CustomerEmail = selectedRow.Cells[2].Value.ToString();
            iops.CustomerPhone = selectedRow.Cells[3].Value.ToString();
            iops.CustomerStatus = Convert.ToBoolean(selectedRow.Cells[4].Value);
        }
 
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