创建方法的问题 [英] problem creating a method

查看:73
本文介绍了创建方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个响应点击事件的数据网格视图,代码工作正常

I have a data grid view which responds to a click event and the code works perfect

private void dgvContactpersonsearch_CellContentClick(object sender, DataGridViewCellEventArgs e)
     {
         SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
         conn.ConnectionString = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
         conn.Open();
         SqlCommand cmd =new SqlCommand();
         string sqlQuery = dgvContactpersonsearch[0, e.RowIndex].Value.ToString();
         sqlQuery = "select * from tblCollectorsregistration where collectorid like ''" + this.dgvContactpersonsearch.Rows[e.RowIndex].Cells[0].Value + "'' ";

         cmd.Connection = conn;
         cmd.CommandText = sqlQuery;
         cmd.CommandType = System.Data.CommandType.Text;
         SqlDataReader dr = null;
         dr = cmd.ExecuteReader();
         while (dr.Read())
         {

             txtCollectorid.Text = dr["collectorid"].ToString();
             cboTitle.Text = dr["title"].ToString();
             txtSurname.Text = dr["surname"].ToString();
             txtMiddlename.Text = dr["middlename"].ToString();
             txtFirstname.Text = dr["firstname"].ToString();

         }
         cmd.Dispose();
         this.dgvContactpersonsearch.Visible = false;
     }



我创建了一个METHOD并将相同的代码复制到其中,以便可以在项目的任何地方调用该方法,但是我得到了错误当前上下文中不存在名称''''请帮帮我


I created a METHOD and copied this same code into it so that the method can be called anywhere in the project but i get the error "The name ''e'' does not exist in the current context" please help me out

public void dgvCPclick()
        {
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            conn.ConnectionString = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
            conn.Open();
            SqlCommand cmd =new SqlCommand();
            string sqlQuery = dgvContactpersonsearch[0, e.RowIndex].Value.ToString();
            sqlQuery = "select * from tblCollectorsregistration where collectorid like '" + this.dgvContactpersonsearch.Rows[e.RowIndex].Cells[0].Value + "' ";
            
            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
            cmd.CommandType = System.Data.CommandType.Text;
            SqlDataReader dr = null;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
               
                txtCollectorid.Text = dr["collectorid"].ToString();
                cboTitle.Text = dr["title"].ToString();
                txtSurname.Text = dr["surname"].ToString();
                txtMiddlename.Text = dr["middlename"].ToString();
                txtFirstname.Text = dr["firstname"].ToString();
                
            }
            cmd.Dispose();
            this.dgvContactpersonsearch.Visible = false;
        }

推荐答案

您需要将DataGridViewCellEventArgs传递给此方法,因为您正在使用事件Args成员数据e.RowIndex在您的点击事件中,如果您将这些操作移动到另一个方法,则将这些值作为参数传递给方法或处理所需的内容。



e的使用情况。此行的RowIndex导致错误
You need to pass the DataGridViewCellEventArgs to this method because you are using the event Args member data e.RowIndex in your click event and if your moving those operation to a different method pass these value as argument to the method or what is needed for your processing.

usage of e.RowIndex at this line cause the error
sqlQuery = "select * from tblCollectorsregistration where collectorid like '" + this.dgvContactpersonsearch.Rows[e.RowIndex].Cells[0].Value + "' ";


这篇关于创建方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆