【问题标题】:Assigning images to rows in data grid view将图像分配给数据网格视图中的行
【发布时间】:2014-06-30 07:23:30
【问题描述】:

我有一个使用 VS 2010 用 C# 编写的 winform 程序。

我通过单击一个来编辑数据网格视图中的行,从行中获取该数据并用它填充文本框,根据需要编辑发送回我的数据网格。

What I want to is have an image related to each row in my data grid view (e.g. a picture of an employee) that would appear in a picture box when that particular row is selected.

简而言之,如果我点击员工 Andrew,就会出现 Andrew 的照片,如果我点击 Rasheed,则会出现 Rasheeds 的照片,依此类推。

图片框位于数据网格视图之外,因此不在另一列中。它只是独立的。

谁能帮我解决这个问题?

选择行并发送到文本框的代码:

// Displays Customer infomation in Update Area
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
        {

        }
        else
        {
            if (customersDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
            {
                textBox_customerID.Text = "" + customersDataGridView.Rows[e.RowIndex].Cells["Customer_ID"].Value.ToString();
                textBox_forename.Text = customersDataGridView.Rows[e.RowIndex].Cells["Forename"].Value.ToString();
                textBox_surname.Text = customersDataGridView.Rows[e.RowIndex].Cells["Surname"].Value.ToString();
                textBox_address.Text = customersDataGridView.Rows[e.RowIndex].Cells["Address"].Value.ToString();
                textBox_town.Text = customersDataGridView.Rows[e.RowIndex].Cells["Town"].Value.ToString();
                textBox_postcode.Text = customersDataGridView.Rows[e.RowIndex].Cells["Postcode"].Value.ToString();
                textBox_dateofbirth.Text = customersDataGridView.Rows[e.RowIndex].Cells["Date_Of_Birth"].Value.ToString();
                textBox_phonenum.Text = customersDataGridView.Rows[e.RowIndex].Cells["Phone_Number"].Value.ToString();
                textBox_email.Text = customersDataGridView.Rows[e.RowIndex].Cells["Email"].Value.ToString();
                textBox_current_rental.Text = customersDataGridView.Rows[e.RowIndex].Cells["Current_Rental"].Value.ToString();
            }   

更新行并发送回数据网格的代码:

DialogResult dialogResult = MessageBox.Show("Are you sure you want to update this customer? ", "Update - " + textBox_forename.Text + textBox_surname.Text, MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {// Updates Product Info
            if (textBox_customerID.Text == "" || textBox_forename.Text == "" || textBox_surname.Text == "" || textBox_address.Text == "" || textBox_town.Text == "" || textBox_postcode.Text == "" || textBox_dateofbirth.Text == "" || textBox_phonenum.Text == "" || textBox_email.Text == "" || textBox_current_rental.Text == "")
            {
                MessageBox.Show("Please leave no boxes blank");
            }
            else
            {
                string custData = textBox_customerID.Text;
                command.CommandText = "UPDATE customers SET Customer_ID = '" + textBox_customerID.Text + "',  Forename = '" + textBox_forename.Text + "', Surname = '" + textBox_surname.Text + "', Address = '"
                    + textBox_address.Text + "', Town = '" + textBox_town.Text + "', Postcode = '" + textBox_postcode.Text + "', Date_Of_Birth = '" + textBox_dateofbirth.Text + "', Phone_Number = '" + textBox_phonenum.Text + "', Email = '" + textBox_email.Text + "', Current_Rental = '" + textBox_current_rental.Text + "' WHERE Customer_ID = '" + custData + "'";
                connect.Open();
                command.Connection = connect;
                command.ExecuteNonQuery();
                connect.Close();
                customersDataGridView.Rows.Clear();
                refreshCustomers();
                MessageBox.Show("Customer Updated");

【问题讨论】:

  • 图片是否驻留在数据网格视图中?在datagridview的隐藏列中?
  • 没有。我在 DGV 外面有一个图片框,如果可能的话,我想使用它。在 DGV 专栏中会更简单吗?
  • 是的,您可以在 DGV 中拥有图像。

标签: c# winforms visual-studio-2010 datagridview


【解决方案1】:

选项 1

您可以在 DataGridView 中添加DataGridViewImageColumn 以在行中显示员工图片。 MSDN 示例在上述指定链接中非常有用

选项 2

  1. 跟踪CellClick事件
  2. 获取行索引int rowIndex = e.RowIndex
  3. 获取DataGridViewRowDataGridViewRow row = DataGridView1.Rows[rowIndex]
  4. 根据行值从数据源中检索数据
  5. 从检索到的数据中将图像加载到图片框中。

Byte[] 中检索到的图像数据可以加载到图片框中。类似的东西:

var data = (Byte[])(datatable.Rows[rowIndex]["EmployeeImage"]);
var stream = new MemoryStream(data);
pictureBox1.Image= Image.FromStream(sream);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2018-02-13
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多