0

次のコードを使用して、telerikグリッドビューで画像を表示しようとしています。

  foreach (var item in radGridView1.Rows)
        {
            try
            {
                item.Cells["column1"].CellElement.Text = "";
                item.Cells["column1"].CellElement.StretchVertically = true;
                item.Cells["column1"].CellElement.ImageLayout = ImageLayout.Zoom;
                item.Cells["column1"].CellElement.ImageAlignment = ContentAlignment.MiddleCenter;
                item.Cells["column1"].CellElement.Image = Image.FromFile("img/1.jpg");
                pictureBox1.Image = Image.FromFile(item.Cells["Picture"].Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

しかし、アプリケーションを実行しようとすると、エラーメッセージが表示されます。

オブジェクト参照がオブジェクト インスタンスに設定されていません

何が悪いのでしょうか?!

4

2 に答える 2

1

nullオブジェクトを逆参照しているように聞こえます(例null.SomeProperty)。

エラーが発生している行にブレークポイントを設定し、null間接参照しているオブジェクトを確認する必要があります。

于 2012-08-04T05:41:10.417 に答える
0

Telerik Virtualizationにより、RadGridView CellElementsやRowElementsなどのコントロールはnullになる可能性があります。したがって、Telerik RadGridViewコントロールなどで特定のセルをフォーマットするには、次のような「CellFormatting」イベントを使用する必要があります。

 private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            try
            {
                if (e.CellElement.ColumnInfo.HeaderText == "Picture") 
                {
                    e.CellElement.Image = pictureBox1.Image;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

もちろん、同じアプローチは、RowFormattingイベントを使用して特定の行をフォーマットする場合にも有効です。

于 2012-08-04T08:37:04.817 に答える