Вопрос

I have a winform program written in C# using VS 2010.

I edit the rows from the data grid view by clicking one, take that data from the row and populate textboxes with it, edit as needed the send back to my data grid.

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.

In short, if I click employee Andrew, Andrew's picture appears, if I click Rasheed, Rasheeds picture appears and so on.

The picture box is outside of the data grid view, so is not in another column. It is just stand alone.

Could anyone give me some help on this?

Code for selecting row and sending to textboxes:

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

Code for updating rows and sending back to data grid:

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");
Это было полезно?

Решение

Option 1:

You can add DataGridViewImageColumn within DataGridView to show employees picture in the row. MSDN example is quite useful in above specified link

Option 2:

  1. Track CellClick event
  2. Get row index int rowIndex = e.RowIndex
  3. Get DataGridViewRow DataGridViewRow row = DataGridView1.Rows[rowIndex]
  4. Retrieve data from your datasource based on row value
  5. Load image into picture box from retrieved data.

Image data retrieved in Byte[] can be loaded into picture box. Something like that:

var data = (Byte[])(datatable.Rows[rowIndex]["EmployeeImage"]);
var stream = new MemoryStream(data);
pictureBox1.Image= Image.FromStream(sream);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top