日期:2014-05-18  浏览次数:21137 次

2个功能,解决马上结贴
之前做ASP.NET
现开始接触WINFORM ,请问下面两项功能如何实现。


1第一列显示图片,按照数据库状态改变图片状态。
2右击出现菜单,点击后执行我设置的方法。

------解决方案--------------------
C# code
用DataGridView控件

  public System.Drawing.Image GetImage(string path)
        {
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
            fs.Close();
            return result;
        }

        private void demoGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
          //这里写判断条件,根据条件显示不同图片
            if (demoGrid.Columns[e.ColumnIndex].Name.Equals("Image"))
            {
                string path = System.Windows.Forms.Application.StartupPath + @"/1.gif";
                e.Value = GetImage(path);
            }
        }

//设置右键菜单
     private void dgvList_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {

                    if (dgvList.Rows[e.RowIndex].Selected == false)
                    {
                        dgvList.ClearSelection();
                        dgvList.Rows[e.RowIndex].Selected = true;
                    }
                    if (dgvList.SelectedRows.Count == 1 && e.ColumnIndex != -1)
                    {
                        dgvList.CurrentCell = dgvList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    dgvList.ContextMenuStrip = contextMenuStrip1;
                }

                else
                {
                    dgvList.ContextMenuStrip = null;
                    return;
                }
            }
        }


//执行方法
  private void tsmDelete_Click(object sender, EventArgs e)
        {
  }

------解决方案--------------------
探讨
引用:
右键菜单可以在 RowsAdded 事件里指定

C# code

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)……