Visual c#读取DataGridView数据并在PictureBox中显示

时间:2021-09-29 15:51:27

im sorry fot being a newbie on this language. Here's my simple situation.

对不起,我是这种语言的新手。这是我的简单情况。

I have a DataGrid where i put my inventory items in this way:

我有一个DataGrid,我以这种方式放置我的库存物品:

 public void UpdateInventoryListUI()
    {
        dGridInvetory.RowHeadersVisible = false;

        dGridInvetory.ColumnCount = 2;
        dGridInvetory.Columns[0].Name = "Name";
        dGridInvetory.Columns[0].Width = 112;
        dGridInvetory.Columns[1].Name = "Quantity";

        dGridInvetory.Rows.Clear();


        foreach (InventoryItem inventoryItem in mainForm1._player.Inventory)
        {
            if (inventoryItem.Quantity > 0)
            {


                dGridInventory.Rows.Add(new[] { inventoryItem.Details.Name, oggettoInventory.Quantity.ToString() });
            }

        }            
    }

Ok it works fine and show me my items. Now i want to create an event that when i select with mouse the Row (entire Row - so the name and the quantity) it shows me in the picture box the image of that item. I need to know how to read the STRING like below:

好吧它工作正常,并告诉我我的项目。现在我想创建一个事件,当我用鼠标选择Row(整行 - 所以名称和数量)时,它会在图片框中显示该项目的图像。我需要知道如何阅读STRING如下:

    private void dGridInventory_MouseClick(object sender, MouseEventArgs e)
    {
          if(// the string "Name" on row is == "Mask_DPS"){
          picBoxMask.Image = Properties.Resources.MASK_DPS;
          labelInfo.Text = "This is a dps Mask!";
    }
          if((// the string "Name" on row is == "Mask_TANK"){
          picBoxMask.Image = Properties.Resources.MASK_TANK;
          labelInfo.Text = "This is a tank mask!;

          //...and so on!
    }

Can you help me please? Just wanna click on the Row and compare the string in the Row. If is the same then show me the image in my picture box.

你能帮我吗?只想点击Row并比较Row中的字符串。如果相同则显示我的图片框中的图像。

Thank all and sry for my bad english.

谢天谢地,我的英语不好。

1 个解决方案

#1


0  

You are not using the best event for what you want to accomplish. Try using the SelectionChanged event instead:

您没有将最佳事件用于您想要完成的任务。请尝试使用SelectionChanged事件:

void dGridInventory_SelectionChanged(object sender, EventArgs e) {
  if (dGridInventory.CurrentRow != null) {
    if (dGridInventory.CurrentRow.Cells["Name"].Value.ToString() == "Mask_DPS") {
      // etc...
    }
  }
}

Make sure the event is properly subscribed to the DataGridView control.

确保事件已正确订阅DataGridView控件。

#1


0  

You are not using the best event for what you want to accomplish. Try using the SelectionChanged event instead:

您没有将最佳事件用于您想要完成的任务。请尝试使用SelectionChanged事件:

void dGridInventory_SelectionChanged(object sender, EventArgs e) {
  if (dGridInventory.CurrentRow != null) {
    if (dGridInventory.CurrentRow.Cells["Name"].Value.ToString() == "Mask_DPS") {
      // etc...
    }
  }
}

Make sure the event is properly subscribed to the DataGridView control.

确保事件已正确订阅DataGridView控件。