2017-09-04 1 views
1

GridView EventHandler 문제를 도와 줄 사람이 있습니까? 이 GridView 내 LINQ 필터로 ID 사용하여 Excel 데이터를 추출 할 각 행에 대한 단추가 있습니다.EventArgs e를 사용하여 선택한 행 데이터 전달

내 문제는 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); 
    }                 
} 

그것은 이벤트를 트리거하지만 난 알고 싶습니다 어떻게하면 클릭이 EventHandler에 그 선택된 행의 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); 
    } 
} 

그리고 objJobs 내 LINQ를 필터링하기 위해 선택한 행 번호 또는 ID를 사용하고 싶습니다.

답변

0

내가 선택한 행의 레코드 ID를 가져 오려고한다고 가정합니다.

아래 코드는 selectedRow의 레코드 ID가 디자인 페이지의 gridview 디자인에있는 경우 제공합니다.

는 논리 아래이 사용을 수행합니다

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); 
      // 


     }                 
    } 
0

는 필요에 따라 각 행에 대해 ID를 검색, 당신은 DataKeyNames 속성 gvJobs에 필요한 ID를 배치 할 수 있습니다.

.ASPX 예

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

.aspx.cs 예 (버튼 내의 온 클릭 이벤트)

Button button = sender as Button; 
GridViewRow gridViewRow = button.NamingContainer as GridViewRow; 
int id = Convert.ToInt32(gvJobs.DataKeys[gridViewRow.RowIndex].Value);