Jorge
Jorge

Reputation: 18237

Problem with datagridView

hi i'm trying to select the the object associated to the row in my datagridview but it doesn't work and i wanna know what i'm doing wrong because for me the code it's fine

        private System.Windows.Forms.DataGridView dataGridView1;
        private Prueba prueba;
        private System.Windows.Forms.BindingSource personaBindingSource2;
        private WindowsApplication1.PruebaTableAdapters.PersonaTableAdapter personaPrueba;

        //this code sets the binding source
        // personaBindingSource2
        // 
        this.personaBindingSource2.DataMember = "Persona";
        this.personaBindingSource2.DataSource = this.prueba;
        // prueba
        this.prueba.DataSetName = "Prueba";
        this.prueba.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
        this.dataGridView1.DataSource = this.personaBindingSource2;

this is the code associated to the event

    //this code it's execute when the users do click in a cell
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewRow row = dataGridView1.CurrentRow;

        Prueba prueba = row.DataBoundItem as Prueba; //this is the part that always have null and i don't know why the row have the values

        MessageBox.Show("hola");
    }

my database table it's like this

table Persona
nombre varchar,
cedula varchar,
IDPersona varchar

i really don't know if a need more code to help me with this trouble but idon't see the problem thanks for your help

Upvotes: 0

Views: 606

Answers (2)

Jorge
Jorge

Reputation: 18237

i solve mi problem with this code my only problem was that i didn't know how to manipulate the data

//gets the current row
DataGridViewRow row = dataGridView1.CurrentRow;

//get's the info of the row
DataRowView algo = row.DataBoundItem as DataRowView;

//set's the type of the data
Prueba.PersonaRow prueba = algo.Row as Prueba.PersonaRow;

now the variable call prueba have the information of the row mapping exactly like the database table but in a object that i can manipulate like this

prueba.Cedula prueba.Nombre

Upvotes: 0

Nik
Nik

Reputation: 7273

Try DataGridView row = dataGridView1.SelectedRows[0]; instead of the CurrentRow.

Upvotes: 2

Related Questions