【问题标题】:How to create Master Detail from datagridview如何从 datagridview 创建主详细信息
【发布时间】:2015-12-27 12:13:25
【问题描述】:

这段代码插入到数据库中

 private void btnSave_Click(object sender, EventArgs e)
    {
        byte[] imageBt = null;
        FileStream fstream = new FileStream(this.txtImgPath.Text,FileMode.Open,FileAccess.Read);
        BinaryReader Br = new BinaryReader(fstream);
        imageBt = Br.ReadBytes((int)fstream.Length);
       // byte[] pic = stream.ToArray();
        try
        {
            conDB.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = conDB;
            command.CommandText = "insert into abaanaCC (CCSpn_CODE,CCFname,CCLname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCImage)" +
                " values ('" + spn_codetxt.Text + "','" + txtfname.Text + "','" + lnametxt.Text + "','" + mnametxt.Text + "','" + DOBDTPicker1.Text + "','" + gendercomboBox.Text + "','" + schtxt.Text + "','" + classcomboBox.Text + "','" + villatxt.Text + "','" + siblingscombobx.Text + "','" + guardiantxt.Text + "','" + contacttxt.Text + "',@IMG) ";
            command.Parameters.Add(new OleDbParameter("@IMG",imageBt));
            //command.Parameters.AddWithValue("@IMG",pic);
            command.ExecuteNonQuery();
            MessageBox.Show("Record Saved");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to save" + ex);
        }
        conDB.Close();
    }

那么这是给datagridview的

private void Update_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'abaanaDataSet.abaanaCC' table. You can move, or remove it, as needed.
        this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);

    }

我正在使用单元格单击事件,这样当单击一个单元格时,就会出现该行的内容,即CCImageCCSpn_CODECCSpn_CODE 出现在 Ptxtspn_code textBox 就好了。问题是我正在转换的 byte[] 图像。它只显示第一行的图像。我如何让PpicBox 显示我点击datagridView 的任何行中的任何图像,就像Ptxtspn_code textBox

 private void abaanaCCDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
            this.Ptxtspn_code.Text = this.abaanaCCDataGridView.SelectedRows[0].Cells[this.dataGridViewTextBoxColumn2.Name].Value.ToString();
this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);           
byte[] mydata = (byte[])this.abaanaDataSet.abaanaCC.Rows[0]["CCImage"];
          MemoryStream stream = new MemoryStream(mydata);           
         this.PpicBox.Image =Image.FromStream(stream);

 }

【问题讨论】:

    标签: c# winforms datagridview master-detail


    【解决方案1】:

    注意:

    • CellClick 事件中,您应该检查点击是否不在行标题或列标题上
    • e.RowIndex是被点击单元格的行索引,e.ColumnIndex是被点击单元格的列索引
    • 要获取点击行的值,您可以使用:
      • yourDGV.Rows[e.RowIndex].Cells["GridColumnName"].value
      • yourDGV.Rows[e.RowIndex].Cells[2].Value
      • ((DataRowView)yourDGV.Rows[e.RowIndex].DataBoundItem)[2]
      • ((DataRowView)yourDGV.Rows[e.RowIndex].DataBoundItem)["DataTableColumnName"]
    • 我不知道为什么要在 CellClick 中填写 abaanaCC,因为您在表单加载事件中加载数据,因此包括 CCImage 在内的所有值都存在于您的数据表中。所以我删除了它。似乎只有当您想要加载数据时才应该填写它而不是在单元格单击中。

    例如你的代码可能是这样的:

    private void abaanaCCDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
            return;
    
        this.Ptxtspn_code.Text = this.abaanaCCDataGridView.Rows[e.RowIndex].Cells[this.dataGridViewTextBoxColumn2.Name].Value.ToString();       
        byte[] mydata = (byte[])this.abaanaDataSet.abaanaCC.Rows[r.RowIndex]["CCImage"];
        MemoryStream stream = new MemoryStream(mydata);           
        this.PpicBox.Image =Image.FromStream(stream);
    }
    

    【讨论】:

    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多