Problem with datagridView

本文关键字:datagridView with Problem | 更新日期: 2023-09-27 17:54:05

嗨,我正在尝试选择与datagridview中的行相关联的对象但它不起作用我想知道我做错了什么因为对我来说代码是好的

        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 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");
    }

我的数据库表是这样的

table Persona
nombre varchar,
cedula varchar,
IDPersona varchar

我真的不知道是否需要更多的代码来帮助我解决这个问题,但我没有看到问题谢谢你的帮助

Problem with datagridView

尝试DataGridView row = dataGridView1.SelectedRows[0];代替CurrentRow

我用这段代码解决了我的问题,我唯一的问题是我不知道如何操作数据

//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;

现在变量调用prueba有行映射的信息就像数据库表一样但是在一个对象中,我可以像这样操作

prueba.Cedulaprueba.Nombre