C# 使用EventArgs e传递所选行数据

C# 使用EventArgs e传递所选行数据,c#,asp.net,gridview,C#,Asp.net,Gridview,有人能帮我解决GridViewEventHandler问题吗?我有一个GridView,它的每一行都有一个按钮,可以使用ID作为我的LINQ的过滤器将数据提取到excel 我的问题是,如何使用EventHandler方法中仅有的两个参数来实现这一点 protected void gvJobs_RowCommand(Object sender, GridViewCommandEventArgs e) { if (e.CommandName == "btnExtract_Click")

有人能帮我解决GridView
EventHandler
问题吗?我有一个GridView,它的每一行都有一个按钮,可以使用
ID
作为我的LINQ的过滤器将数据提取到excel

我的问题是,如何使用
EventHandler
方法中仅有的两个参数来实现这一点

protected void gvJobs_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "btnExtract_Click")
    {                                                                
         int index = Convert.ToInt32(e.CommandArgument);
         GridViewRow row = gvJobs.Rows[index];

         //EventHandler call
         ExtractJobsCalibrationForm(sender, e);
    }                                                                
}
它会触发事件,但我想知道单击的特定行按钮的数据是否会将所选行的ID传递给
EventHandler

以下是我的
事件处理程序的代码

void View_ExtractJobsCalibrationForm(object sender, EventArgs e) 
{
    try
    {
        using (Data.DataContexts.IDataContext objContext = Data.DataContexts.DataContext.CreateDataContext()) 
        {
            IQueryable<Data.JobSummary> objJobs = objContext.Jobs.GetJobSummaries().Where(j => !j.IsDeleted);

            string path = HttpContext.Current.Server.MapPath("/Reports/ExcelTemplate/Calibration_Form_ARS-FORM-CL1_Template.xlsx");
            string destPath = @"C:\Users\ringgo_dejesus\Desktop\Calibration_Form_ARS-FORM-CL1_Template.xlsx";


            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;
            oXL.DisplayAlerts = false;

            //Create a copy from the Template to save the data.
            System.IO.File.Copy(path, destPath, true);

            //Open the copied template.
            mWorkBook = oXL.Workbooks.Open(destPath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            //Get all the sheets in the workbook
            mWorkSheets = mWorkBook.Worksheets;
            //Get the allready exists sheet
            mWSheet = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Pre calibration Check in");

            //Microsoft.Office.Interop.Excel.Range range = mWSheet.UsedRange;
            //int colCount = range.Columns.Count;
            //int rowCount = range.Rows.Count;

            //for (int index = 1; index < 15; index++)
            //{
            //    mWSheet.Cells[rowCount + index, 1] = rowCount + index;
            //    mWSheet.Cells[rowCount + index, 2] = "New Item" + index;
            //}

            Excel.Range JobNo = (Excel.Range)mWSheet.Rows.Cells[2, 2];
            Excel.Range Date = (Excel.Range)mWSheet.Rows.Cells[3, 2];
            Excel.Range CheckedInBy = (Excel.Range)mWSheet.Rows.Cells[4, 2];
            Excel.Range ClientName = (Excel.Range)mWSheet.Rows.Cells[7, 2];
            Excel.Range ClientNum = (Excel.Range)mWSheet.Rows.Cells[8, 2];
            Excel.Range PONumber = (Excel.Range)mWSheet.Rows.Cells[9, 2];
            Excel.Range MonitorManufacturer = (Excel.Range)mWSheet.Rows.Cells[22, 4];
            Excel.Range MonitorModel = (Excel.Range)mWSheet.Rows.Cells[23, 4];
            Excel.Range MonitorSerialNum = (Excel.Range)mWSheet.Rows.Cells[24, 4];
            Excel.Range ProbeManufacturer = (Excel.Range)mWSheet.Rows.Cells[26, 4];
            Excel.Range ProbeModel = (Excel.Range)mWSheet.Rows.Cells[27, 4];
            Excel.Range ProbeSerialNum = (Excel.Range)mWSheet.Rows.Cells[28, 4];
            Excel.Range LastCalibrated = (Excel.Range)mWSheet.Rows.Cells[37, 4];

            var data = objJobs.Where(j => j.JobReference.Contains(View.JobReferenceNo.Trim()))
                              .Select(x => new
                              {
                                  JobNum = x.JobReference,
                                  CreationDate = x.DateCreated,
                                  CreatedBy = x.CreatedByUserId,
                                  ClientName = x.ClientName,
                                  ClientNumber = x.ClientId,
                                  PONumber = x.PurchaseOrderNumber
                              }).FirstOrDefault();

            JobNo.Value = data.JobNum;
            Date.Value = data.CreationDate;
            CheckedInBy.Value = data.CreatedBy;
            ClientName.Value = data.ClientName;
            ClientNum.Value = data.ClientNumber;
            PONumber.Value = data.PONumber;

            mWorkBook.Save();
            //mWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            //mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
            mWSheet = null;
            mWorkBook = null;
            //oXL.Quit();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
    catch (Exception objException)
    {
        View.DisplayException(objException);
    }
}
void视图\u ExtractJobsCalibrationForm(对象发送方,事件参数e)
{
尝试
{
使用(Data.DataContext.IDataContext objContext=Data.DataContext.DataContext.CreateDataContext())
{
IQueryable objJobs=objContext.Jobs.GetJobSummaries()。其中(j=>!j.IsDeleted);
string path=HttpContext.Current.Server.MapPath(“/Reports/ExcelTemplate/Calibration_Form_ARS-Form-CL1_Template.xlsx”);
字符串destPath=@“C:\Users\ringgo\u dejesus\Desktop\Calibration\u Form\u ARS-Form-CL1\u Template.xlsx”;
oXL=新的Microsoft.Office.Interop.Excel.Application();
可见=真;
oXL.DisplayAlerts=false;
//从模板创建副本以保存数据。
System.IO.File.Copy(path,destPath,true);
//打开复制的模板。
mWorkBook=oXL.Workbooks.Open(destPath,0,false,5,“,”,false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“,true,false,0,true,false,false);
//获取工作簿中的所有工作表
mWorkSheets=mWorkBook.工作表;
//获取allready表
mWSheet=(Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_项(“校准前签入”);
//Microsoft.Office.Interop.Excel.Range=mWSheet.UsedRange;
//int colCount=range.Columns.Count;
//int rowCount=range.Rows.Count;
//对于(int-index=1;index<15;index++)
//{
//mWSheet.Cells[行计数+索引,1]=行计数+索引;
//mWSheet.Cells[行数+索引,2]=“新项目”+索引;
//}
Excel.Range作业编号=(Excel.Range)mWSheet.Rows.Cells[2,2];
Excel.Range日期=(Excel.Range)mWSheet.Rows.Cells[3,2];
Excel.Range checkedBy=(Excel.Range)mWSheet.Rows.Cells[4,2];
Excel.Range ClientName=(Excel.Range)mWSheet.Rows.Cells[7,2];
Excel.Range ClientNum=(Excel.Range)mWSheet.Rows.Cells[8,2];
Excel.Range-PONumber=(Excel.Range)mWSheet.Rows.Cells[9,2];
Excel.Range MonitorManufacturer=(Excel.Range)mWSheet.Rows.Cells[22,4];
Excel.Range MonitorModel=(Excel.Range)mWSheet.Rows.Cells[23,4];
Excel.Range MonitorSerialNum=(Excel.Range)mWSheet.Rows.Cells[24,4];
Excel.Range ProbeManufacturer=(Excel.Range)mWSheet.Rows.Cells[26,4];
Excel.Range ProbeModel=(Excel.Range)mWSheet.Rows.Cells[27,4];
Excel.Range ProbeSerialNum=(Excel.Range)mWSheet.Rows.Cells[28,4];
Excel.Range LastCalibrated=(Excel.Range)mWSheet.Rows.Cells[37,4];
var data=objJobs.Where(j=>j.JobReference.Contains(View.JobReferenceNo.Trim())
.选择(x=>new
{
JobNum=x.JobReference,
CreationDate=x.DateCreated,
CreatedBy=x.CreatedByUserId,
ClientName=x.ClientName,
ClientNumber=x.ClientId,
PONumber=x.PurchaseOrderNumber
}).FirstOrDefault();
JobNo.Value=data.JobNum;
Date.Value=data.CreationDate;
CheckedBy.Value=data.CreatedBy;
ClientName.Value=data.ClientName;
ClientNum.Value=data.ClientNumber;
PONumber.Value=data.PONumber;
mWorkBook.Save();
//mWorkBook.SaveAs(路径,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);
//mWorkBook.Close(Missing.Value,Missing.Value,Missing.Value);
mWSheet=null;
mWorkBook=null;
//oXL.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
捕获(异常对象异常)
{
View.DisplayException(objException);
}
}

我想使用所选的行号或Id来过滤我的LINQ中的
objJobs

我想您应该要获取所选行的记录Id

下面的代码将为您提供selectedRow的记录ID(如果它出现在gridview的design in design页面中)

为此,请使用以下逻辑:

protected void gvJobs_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btnExtract_Click")
        {    
             int yourSelectedID = (((Button)e.CommandSource).NamingContainer as GridViewRow).RowIndex;     
             //
                  //do stuff with yourSelectedID
             //EventHandler call
             ExtractJobsCalibrationForm(yourSelectedID);
             //  


        }                                                                
    }

我想您想要获取所选行的记录ID

下面的代码将为您提供selectedRow的记录ID(如果它出现在gridview的design in design页面中)

为此,请使用以下逻辑:

protected void gvJobs_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btnExtract_Click")
        {    
             int yourSelectedID = (((Button)e.CommandSource).NamingContainer as GridViewRow).RowIndex;     
             //
                  //do stuff with yourSelectedID
             //EventHandler call
             ExtractJobsCalibrationForm(yourSelectedID);
             //  


        }                                                                
    }

您可以将所需的ID放入gvJobs DataKeyNames属性中,然后根据需要检索每行的ID

.aspx示例

<asp:GridView ID="gvJobs" runat="server" DataKeyNames="YourIdNameGoesHere" />

您可以将所需的ID放入gvJobs DataKeyNames属性中,然后根据需要检索每行的ID

.aspx示例

<asp:GridView ID="gvJobs" runat="server" DataKeyNames="YourIdNameGoesHere" />