2017-02-21 92 views
0

我在C#中的winform應用程序與datagridview女巫得到it's從sql數據庫中的值,但是當我從德datagridview點擊一行得到的數據顯示在文本框進行編輯。 問題是可以編輯的一列是圖像。 我可以上傳圖片,看看它在DataGrid但是當我集團公司的的rowHeader選擇我得到一個錯誤:代碼是 「的不正確的格式輸入字符的字符串」:圖片圖片框

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); 

    MemoryStream ms = new MemoryStream((byte[])dataGridView1.CurrentRow.Cells[2].Value); 
    pictureBox1.Image = Image.FromStream(ms); 
    desc2.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); 
    tipo.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString(); 
    prumos.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString(); 
} 

[進入image description here] [1]] [1] 在此先感謝,

+0

在哪條確切的線路上會得到異常?它似乎與'Convert.ToInt32'相關,比圖片更多... –

+0

@OfirWinegarten不幸的是系統不會拋出異常。 我只收到錯誤,表單關閉,然後我回到以前的表單。 – septaug

+0

@OfirWinegarten我想評論的轉換線,現在我得到的錯誤:無法System.String類型的對象關聯到System.byte – septaug

回答

1

您的第一個問題來自Convert.ToInt32,與Image無關。確保文本僅包含一個數字。

與形象的第二個問題可以這樣做:

var imageCell = (DataGridViewImageCell)dataGridView1.CurrentRow.Cells[2]; 
pictureBox1.Image = (Image)imageCell.Value; 

UPDATE - 以上是錯誤

的問題是錯誤的指標,像寫在註釋: 正確代碼是:

MemoryStream ms = new MemoryStream((byte[])dataGridView1.CurrentRow.Cells[0].Value‌​); 
pictureBox1.Image = Image.FromStream(ms); 
+0

我想你的溶液,但我在線得到一個錯誤的所有文本: pictureBox1.Image = Image.FromStream((圖像)imageCell.Value); 不能從'system.drawings.image轉換的System.IO.Stream – septaug

+0

哎呀有一個錯誤..現在它應該工作 –

+0

哎呀仍然得到同樣的錯誤:\ 生病從形式上傳的截圖,也許it's另一原因 – septaug