2017-09-04 13 views
1

GridView EventHandlerの問題を教えてもらえますか?私はこのグリッドビューには、LINQのフィルタとしてIDを使用してExcelにデータを抽出する各行のボタンがあります。EventArgs eを使用して選択した行データを渡す

私の問題は、EventHandlerのメソッドに存在する唯一の2つのパラメータを使用して、どうやって行うのでしょうか?

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がデザインページのグリッドビューのデザインに存在する場合、それが表示されます。あなたは、必要に応じて行ごとにIDを取得し、その後、DataKeyNames属性gvJobsに必要なIDを置くことができる

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

:ロジックの下にこの使用を行うには

の.aspx例

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

.aspx.cs例(ボタンOnClickイベント内)

Button button = sender as Button; 
GridViewRow gridViewRow = button.NamingContainer as GridViewRow; 
int id = Convert.ToInt32(gvJobs.DataKeys[gridViewRow.RowIndex].Value); 
関連する問題