如何从datagridview创建Master Detail

本文关键字:Master Detail 创建 datagridview | 更新日期: 2023-09-27 18:15:23

这段代码插入到数据库

 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_CODE)就会出现。CCSpn_CODE出现在Ptxtspn_code textBox中正好。问题是我正在转换的字节[]图像。它只显示第一行的图像。我怎么能让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);
 }

如何从datagridview创建Master Detail

备注:

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