简体   繁体   中英

how to display selected row in textbox MVP c#

I have MVP project iam trying to display the selected row to 3 textboxs and 1 checkbox: i know i can do it from the CellClick event like so:

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 iam 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...

Salem, for what I can see in your code, and based on your explanation you have an event click and you want to trigger that one from the presenter itself, so I think this is not a correct approach, I will go by extracting the method from the click and calling from the presenter the new method, I guess you will need a way to get the Index from the presenter and pass it as an argument.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int index = e.RowIndex;// get the Row Index
    CheckRowBoxByID(index);
}

void FillDataWithRowByID(int 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);
}

This is a high overview, it could be incorrect but I'll go with having a segregation of responsibilities. and from the presenter call below method.

FillDataWithRowByID(withTheIntID);

Basically the errors you get it are because:

  1. You're trying to create an event that doesn't exist in the presenter.
  2. Passing an Objet as a Row.
  3. Passing a String as Integer

Hopes it makes sense, Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM