有人可以帮我解决GridView EventHandler
问题吗?我有这个GridView,每行都有一个按钮,使用ID
作为过滤器将数据提取到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
。
以下是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);
}
}
我想使用选定的行号或ID来过滤objJobs
的LINQ。
答案 0 :(得分: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);
//
}
}
答案 1 :(得分:0)
您可以将所需的ID放入gvJobs DataKeyNames属性中,然后根据需要检索每行的ID。
.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);