【问题标题】:Visual c# Read DataGridView data and show in PictureBoxVisual c#读取DataGridView数据并显示在PictureBox中
【发布时间】:2016-07-08 06:26:06
【问题描述】:

我很抱歉成为这门语言的新手。这是我的简单情况。

我有一个 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() });
            }

        }            
    }

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

    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!
    }

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

感谢大家,并为我糟糕的英语感到抱歉。

【问题讨论】:

    标签: c# visual-studio datagridview picturebox


    【解决方案1】:

    你没有使用最好的活动来完成你想要完成的事情。尝试改用 SelectionChanged 事件:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-07
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多