如何阅读每一行网格

时间:2017-08-30 07:42:45

标签: asp.net

我试图在网格的每一行上使用工具提示,以便在用户将指针放在特定单元格上时显示详细信息。它应该显示每一行的详细信息,但它只显示第一行的详细信息。任何人都可以帮助我吗?

for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
    if (e.Row.Cells[i].Text == "0" || string.IsNullOrEmpty(e.Row.Cells[i].Text) || e.Row.Cells[i].Text == "&nbsp;")
    {
        e.Row.Cells[i].Text = "";
    }
    else
    {
        e.Row.Cells[i].BackColor = System.Drawing.Color.Blue;

        dateSetExport.Tables.Clear();
        dateSetExport.Reset();
        SqlParameter[] param = new SqlParameter[2];
        param[1] = new SqlParameter("@Startdate", gvDetails.HeaderRow.Cells[i].Text);
        param[0] = new SqlParameter("@Employe_Id", e.Row.Cells[0].Text.Split('-')[0]);
        DataTable dt1 = DataHelper.getDataTableExecuteSP("usp_GetToolTip", param);
        dt1.TableName = "ToolTip";
        dateSetExport.Tables.Add(dt1);
        string tooltip = "";
        for (int j = 0; j < dt1.Rows.Count; j++)
        {
            tooltip = tooltip + dt1.Rows[j]["normal_working_hours"].ToString() + " Hours : " + dt1.Rows[j]["description"].ToString()+"\n\n";
        }
        e.Row.Cells[i].ToolTip = tooltip;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用另一个foreach来循环所有行:

foreach (GridViewRow row in GridView.Rows)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
       //do your staff
   }
}

但对我来说,你应该使用GridView_RowDataBound事件:

 protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
 {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
      {
        if (e.Row.Cells[i].Text == "0" || string.IsNullOrEmpty(e.Row.Cells  [i].Text) || e.Row.Cells[i].Text == "&nbsp;")
         {
    e.Row.Cells[i].Text = "";
          }
    else
    {
        e.Row.Cells[i].BackColor = System.Drawing.Color.Blue;

        dateSetExport.Tables.Clear();
        dateSetExport.Reset();
        SqlParameter[] param = new SqlParameter[2];
        param[1] = new SqlParameter("@Startdate", gvDetails.HeaderRow.Cells[i].Text);
        param[0] = new SqlParameter("@Employe_Id", e.Row.Cells[0].Text.Split('-')[0]);
        DataTable dt1 = DataHelper.getDataTableExecuteSP("usp_GetToolTip", param);
        dt1.TableName = "ToolTip";
        dateSetExport.Tables.Add(dt1);
        string tooltip = "";
        for (int j = 0; j < dt1.Rows.Count; j++)
        {
        tooltip = tooltip + dt1.Rows[j]["normal_working_hours"].ToString() + " Hours : " + dt1.Rows[j]["description"].ToString()+"\n\n";
        }`enter code here`
        e.Row.Cells[i].ToolTip = tooltip;
       }
     }
    }
   }