dataGridView1_CellContentClick事件 
        public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string str_class;
            str_class = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            count++;
            this.cdTableAdapter.Adapter.SelectCommand.CommandText = "select name from cd where type=\"" + str_type + "\" and class=\"" + str_class + "\" order by code desc";
            this.cdTableAdapter.Fill(this.memuDataSet.cd);
            if (Rowcnt == 0)
            {
                this.cdTableAdapter.Adapter.SelectCommand.CommandText = "select name from cd where type=\"" + str_type + "\" and s_men=\"" + str_class + "\" or class=\"" + str_class + "\" order by code desc";
                this.cdTableAdapter.Fill(this.memuDataSet.cd);
            }
            
        }我想在事件外调用调用str_class,目的是为了实现        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.cdTableAdapter.Adapter.UpdateCommand.CommandText = "update cd set type=\"DEL\" where name=\"" + str_class + "\"";
        }
部分变量
        public int Rowcnt 
        {
            get 
            {
                return dataGridView1.RowCount;
            }
        }
        public int count=0; 
        private string n
        {
            get
            {
                if (TB=="XF")
                switch (count)
                { case 1: return "B"; 
                  case 2: return "C";
                  case 3: return "D";
                  case 4: return "E";
                  case 5: return "F";
                  case 6: return "G";
                  default: return "A";
                }
                else
                    switch (count)
                    {
                        case 1: return "A";
                        case 2: return "B";
                        case 3: return "C";
                        case 4: return "D";
                        case 5: return "E";
                        case 6: return "F";
                        default: return "";
                    }
            }
        }
        public string str_type
        {
            get
            {
                if (Rowcnt == 0)
                    return "节目";
                else
                    if (TB == "ST")
                        if (count == 0)
                            return "SYS";
                return "菜单" + n;
            }  
        }

解决方案 »

  1.   

    在窗体类中,增加一个属性public class FrmMain 
    {   //
       private string _str_class=string.Empty;
       public string StrClass
      {
    set{_str_class=value;}
    get{return _str_class;}
      }
     
    public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                string str_class;
                str_class = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                count++;
                this.cdTableAdapter.Adapter.SelectCommand.CommandText = "select name from cd where type=\"" + str_type + "\" and class=\"" + str_class + "\" order by code desc";
                this.cdTableAdapter.Fill(this.memuDataSet.cd);
                if (Rowcnt == 0)
                {
                    this.cdTableAdapter.Adapter.SelectCommand.CommandText = "select name from cd where type=\"" + str_type + "\" and s_men=\"" + str_class + "\" or class=\"" + str_class + "\" order by code desc";
                    this.cdTableAdapter.Fill(this.memuDataSet.cd);
                }
                this.StrClass=str_class; //这样就行了,//当然简单一点的直接定义成窗体类里面的私有变量也行
            } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.cdTableAdapter.Adapter.UpdateCommand.CommandText = "update cd set type=\"DEL\" where name=\"" + this.StrClass + "\"";
            }