本文介绍了如何创建从DataGridView主从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库

 私人无效btnSave_Click(对象发件人,EventArgs五)
{$ B此代码插入$ b字节[] imageBt = NULL;
的FileStream fstream的=新的FileStream(this.txtImgPath.Text,FileMode.Open,FileAccess.Read);
BinaryReader BR =新BinaryReader(fstream的);
imageBt = Br.ReadBytes((INT)fstream.Length);
//字节[] PIC = stream.ToArray();

{
conDB.Open();
OleDbCommand的命令=新的OleDbCommand();
command.Connection = conDB;
command.CommandText =插入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(新OleDbParameter(@ IMG,imageBt));
//command.Parameters.AddWithValue(\"@IMG\",pic);
command.ExecuteNonQuery();
MessageBox.Show(记录保存);
}
赶上(异常前)
{
MessageBox.Show(无法保存+ EX);
}
conDB.Close();
}



那么这是DataGridView的

 私人无效Update_Load(对象发件人,EventArgs五)
{
// TODO:这行代码加载数据到abaanaDataSet.abaanaCC'表。您可以移动,或删除它,根据需要。
this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);

}



现在用的细胞点击事件,例如,当一个细胞被点击时,该行的内容,那就是 CCImage CCSpn_CODE 出现。在 CCSpn_CODE 出现在 Ptxtspn_code 的textBox 就好了。问题是字节[]的图像,我是转换。它显示第一行的只有图像。我怎样才能让 PpicBox 显示无论从任何行我点击任何图像 DataGridView的就像 Ptxtspn_code 的textBox

 私人无效abaanaCCDataGridView_CellClick(对象发件人,DataGridViewCellEventArgs E)
{
this.Ptxtspn_code.Text = this.abaanaCCDataGridView.SelectedRows [0] .Cells [this.dataGridViewTextBoxColumn2.Name] .Value.ToString();
this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);
字节[] = MYDATA(字节[])this.abaanaDataSet.abaanaCC.Rows [0] [CCImage];
MemoryStream的流=新的MemoryStream(MYDATA);
this.PpicBox.Image = Image.FromStream(流);

}


解决方案

注意:




  • CellClick 事件,你应该检查是否点击不在行标题或列标题

  • e.RowIndex 是单击单元格,电子商务行索引.ColumnIndex

  • 要获得点击行的值单击的单元格列索引,您可以使用:


    • yourDGV.Rows [e.RowIndex] .Cells [GridColumnName]。值

    • yourDGV.Rows [e.RowIndex] .Cells [2] .value的

    • ((DataRowView的)yourDGV.Rows [E .RowIndex] .DataBoundItem)[2]

    • ((DataRowView的)yourDGV.Rows [e.RowIndex] .DataBoundItem)[ DataTableColumnName]


  • 我没有,为什么你填写abaanaCC在<$ C $任何想法C> CellClick ,因为你在加载窗体加载事件和数据,以便包括CCImage所有值存在ID的数据表。所以我删除它。看来你应该填写它,只有当你想在单元格点击这里就不加载数据。



例如你的代码可能会喜欢这个

 私人无效abaanaCCDataGridView_CellClick(对象发件人,DataGridViewCellEventArgs E)
{
如果(e.RowIndex< 0 || e.ColumnIndex℃下)
的回报;

this.Ptxtspn_code.Text = this.abaanaCCDataGridView.Rows [e.RowIndex] .Cells [this.dataGridViewTextBoxColumn2.Name] .Value.ToString();
字节[] = MYDATA(字节[])this.abaanaDataSet.abaanaCC.Rows [r.RowIndex] [CCImage];
MemoryStream的流=新的MemoryStream(MYDATA);
this.PpicBox.Image = Image.FromStream(流);
}


this code inserts in the database

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

then this is for 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);

    }

am using the cell click event, such that when a cell is clicked, the contents of that row, that is the CCImage and CCSpn_CODE appear. The CCSpn_CODE appears in Ptxtspn_code textBox just fine. The problem is the byte[] image that i'm converting. it's displaying only the image of the first row. how can i make PpicBox display whatever image from whatever row i click on the datagridViewjust like 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);

 }
解决方案

Note:

  • In CellClick event you should check if click is not on row header or column header
  • e.RowIndex is row index of clicked cell, and e.ColumnIndex is index of column of clicked cell
  • To get values of clicked row you can use:
    • 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"]
  • I didn't have any idea about why you fill abaanaCC in CellClick because you load data in form load event and so all values including CCImage is present id your data table. So I removed it. It seems that you should fill it only when you want to load data not here in cell click.

For example your code may like this:

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

这篇关于如何创建从DataGridView主从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:19