将datatable绑定到gridview时,不显示emptydatatext

时间:2013-04-17 10:34:20

标签: asp.net

我正在创建一个数据表并动态添加行,并且当没有记录显示为没有使用emptydatatext找到的记录时,我将数据绑定到gridview,但这不起作用。这是我的代码

  protected void show_fence_report(object sender, EventArgs e)
  {

    int drp_fence_id              = common.make_int(common.get_request("drp_fence"));
    string fence_start_date_time1 = common.get_request("fence_start_date_time");
    string fence_end_date_time1   = common.get_request("fence_end_date_time");

    //Delete data from datatable and gridview
    dt_fence.Clear();

    if (gridview_fence.DataSource != null)
    {
        ((DataView)gridview_fence.DataSource).Table.Clear();
    }
    gridview_fence.DataBind();
    gridview_fence.EmptyDataText = "Records Not Found";

    hid_fence_id.Value = drp_fence_id.ToString();
    hid_fence_start_datetime.Value = fence_start_date_time1;
    hid_fence_end_datetime.Value = fence_end_date_time1;

    display_fence_report();

    gridview_fence.EmptyDataText = "Records Not Found";

}

public void display_fence_report()
{

    string fence_id1 = "", fence_name1, fence_type1 = "", fence_status;
    float default_size = 100000, landmark_latitude1 = 0, landmark_longitude1 = 0, fence_latitude1 = 0, fence_longitude1 = 0, fence_size1 = 0, longitude_x = 0, latitude_y = 0;
    int points_polygon = 0;      


    ArrayList vertices_x = new ArrayList();
    ArrayList vertices_y = new ArrayList();

    query = "select latitude,longitude,added_date_time,speed,location,(select object_value from tracking_master_objects where object_id = a.object_id) as object from tracking_data_domain a where object_id in(select object_id from tracking_assign_fence a where fence_id = '" + hid_fence_id.Value + "' and is_active = '1') and (added_date_time between convert(datetime, '" + hid_fence_start_datetime.Value + "', 105) and convert(datetime, '" + hid_fence_end_datetime.Value + "', 105)) order by gps_id asc";

    dr = common.run_query(query, db, cm, dr, 0);
    if (dr.HasRows)
    {
        //To Build Fence Latitudes and Longitudes
        query = "select fence_id,fence_name,fence_type,landmark_latitude,landmark_longitude,fence_latitude,fence_longitude,fence_size from tracking_master_fences where domain_id = '" + domain_id1 + "' and fence_id = '" + hid_fence_id.Value + "' and is_active = '1'";

        dr2 = common.get_row(query, db2, cm2, dr2, 0);
        if (dr2.HasRows)
        {

            fence_id1 = dr2["fence_id"].ToString();
            fence_name1 = dr2["fence_name"].ToString();
            fence_type1 = dr2["fence_type"].ToString();
            landmark_latitude1 = common.make_float(dr2["landmark_latitude"].ToString());//fs_lat
            landmark_longitude1 = common.make_float(dr2["landmark_longitude"].ToString());//fs_long
            fence_latitude1 = common.make_float(dr2["fence_latitude"].ToString());//sec_lat
            fence_longitude1 = common.make_float(dr2["fence_longitude"].ToString());//sec_long
            fence_size1 = common.make_float(dr2["fence_size"].ToString());

        }
        dr2.Close();

        //Build POlygon Vertices
        if (fence_type1 == "4")
        {


            query = "select polygon_latitude,polygon_longitude from tracking_master_fence_polygons where fence_id = '" + hid_fence_id.Value + "' and domain_id = '" + domain_id1 + "'";
            dr1 = common.run_query(query, db1, cm1, dr1, 0);
            if (dr1.HasRows)
            {
                while (dr1.Read())
                {
                    vertices_x.Add(dr1["polygon_latitude"]);
                    vertices_y.Add(dr1["polygon_longitude"]);
                }
            }
            dr1.Close();
            points_polygon = vertices_x.Count;

        }

        //Create a Datatable of 14 rows
        dt_fence.Columns.Add("fence_id", typeof(string));
        dt_fence.Columns.Add("Date", typeof(string));
        dt_fence.Columns.Add("Speed", typeof(string));
        dt_fence.Columns.Add("Location", typeof(string));
        dt_fence.Columns.Add("landmark_latitude", typeof(string));
        dt_fence.Columns.Add("landmark_longitude", typeof(string));
        dt_fence.Columns.Add("fence_latitude", typeof(string));
        dt_fence.Columns.Add("fence_longitude", typeof(string));
        dt_fence.Columns.Add("latitude", typeof(string));
        dt_fence.Columns.Add("longitude", typeof(string));
        dt_fence.Columns.Add("fence_size", typeof(string));
        dt_fence.Columns.Add("fence_type", typeof(string));
        dt_fence.Columns.Add("fence_status", typeof(string));
        dt_fence.Columns.Add("object", typeof(string));


        while (dr.Read())
        {
            fence_status = "";
            float latitude = common.make_float(dr["latitude"].ToString());
            float longitude = common.make_float(dr["longitude"].ToString());

            if (fence_type1 == "1" || fence_type1 == "2")
            {

                if (((landmark_latitude1 * 100000) < (latitude * 100000) && (fence_latitude1 * 100000) > (latitude * 100000)) && ((landmark_longitude1 * 100000) < (longitude * 100000) && (fence_longitude1 * 100000) > (longitude * 100000)))
                {
                    fence_status = "Inside";
                }
                else
                {
                    fence_status = "Outside";
                }

            }
            else if (fence_type1 == "3")
            {
                float ft = ((latitude * 100000) - (landmark_latitude1 * 100000));
                float st = ((longitude * 100000) - (landmark_longitude1 * 100000));

                float sqrt = common.make_float(Math.Sqrt((ft * ft) + (st * st)).ToString());

                if (fence_size1 < sqrt)
                {
                    fence_status = "Out Side";
                }
                else
                {
                    fence_status = "In Side";
                }
            }
            else if (fence_type1 == "4")
            {
                longitude_x = common.make_float(dr["latitude"].ToString());
                latitude_y = common.make_float(dr["longitude"].ToString());

                int i = 0, j = 0, c = 0;
                for (i = 0, j = points_polygon - 1; i < points_polygon; j = i++)
                {
                    float vertices_y_i_val = common.make_float(vertices_y[i].ToString());
                    float vertices_y_j_val = common.make_float(vertices_y[j].ToString());
                    float vertices_x_i_val = common.make_float(vertices_x[i].ToString());
                    float vertices_x_j_val = common.make_float(vertices_x[j].ToString());

                    if (((vertices_y_i_val > latitude_y != (vertices_y_j_val > latitude_y)) &&
(longitude_x < (vertices_x_j_val - vertices_x_i_val) * (latitude_y - vertices_y_i_val) / (vertices_y_j_val - vertices_y_i_val) + vertices_x_i_val)))
                    {
                        c = 1;
                    }

                }

                if (c == 1)
                {
                    fence_status = "In Side";
                }
                else
                {
                    fence_status = "Out Side";
                }
            }

            dt_fence.Rows.Add(fence_id1, dr["added_date_time"], dr["speed"], dr["location"], landmark_latitude1, landmark_longitude1, fence_latitude1, fence_longitude1, latitude, longitude, fence_size1, fence_type1, fence_status, dr["object"]);

        }//End of while Loop

        //hid_ds_fence.Value = dt_fence.ToString();
        gridview_fence.DataSource = dt_fence;
        gridview_fence.DataBind();


    }
    dr.Close();

    fence_modalpopup.Show();

}

2 个答案:

答案 0 :(得分:1)

在您的代码中,您在DataBind之后分配EmptyDataText值。它应该在DataBind之前。如果“消息”不会改变,您应该按照建议在HTML源代码中定义。

答案 1 :(得分:0)

你可以使用这样的东西

  <Columns>

    <Columns>
<EmptyDataTemplate>
        <asp:Label ID="lblEmptydata" runat="server" Text="No Data here"></asp:Label>
      </EmptyDataTemplate>
                        </asp:GridView>

或者可以试试这个

in my remark gridview's row_databoundevent i wrote

if (e.Row.Cells[0].Text == "&nbsp;") // to display text when there is no data

{
e.Row.Cells[0].Text = "No Data Found";

}

或者也可以尝试这个

dt.Rows.Add(dt.NewRow());
                    grid1.DataSource = dt;
                    grid1.DataBind();
                    int totalcolums = grid1.Rows[0].Cells.Count;
                    grid1.Rows[0].Cells.Clear();
                    grid1.Rows[0].Cells.Add(new TableCell());
                    grid1.Rows[0].Cells[0].ColumnSpan = totalcolums;
                    grid1.Rows[0].Cells[0].Text = "No Data Found";