【问题标题】:Bind New Column To GridView C# **UPDATED**将新列绑定到 GridView C# **更新**
【发布时间】:2016-04-14 10:20:47
【问题描述】:

好的更新这个,因为我今天下午走得更远了..

我已将我的代码合并在一起,但它仍然不正确,我添加了一张图片,让你们正确了解我正在尝试做什么以及它的样子。

结果要换新行了??我想我需要添加一个匹配项,因此如果我的数组列表中的 IP 地址与当前 Gridview 中的 IP 地址匹配,则将它们正确排列在同一行中。

这是我的代码...

protected void Page_Load(object sender, EventArgs e)
        {
            PullData();
        }
        public void PullData()
        {            
            string SQLRET = "SELECT RX_ID, ShopID, Primary_IP, ServiceType, Hardware FROM RouterHealthCheck";

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            conn.Open();

            SqlDataAdapter da = new SqlDataAdapter(SQLRET, conn);
            DataTable dt = new DataTable();
            dt.Columns.Add("Results", typeof(string));
            da.Fill(dt);


            ArrayList IPAddresses = new ArrayList(GetList());

            List<string> Results = new List<string>();

            foreach (string IPAddress in IPAddresses)
            {
                Ping ping = new Ping();
                PingReply pingreply = ping.Send(IPAddress);

                if (pingreply.Status == IPStatus.Success)
                {
                    Results.Add(string.Format("Success"));
                }
                else
                {
                    Results.Add(string.Format("Offline"));
                }
            }
            foreach (string results in Results)
            {                
                dt.Rows.Add(results);
            }            

            if (dt.Columns.Count > 0)
            {

                GridViewRouters.DataSource = dt;
                GridViewRouters.DataBind();
            }
            else
            {                
                DataTable dts = new DataTable();
                GridViewRouters.DataSource = dts;
                GridViewRouters.DataBind();
            }

            conn.Close();
        }

        public ArrayList GetList()
        {
            ArrayList GetList = new ArrayList();

            string GETIP = "SELECT Primary_IP FROM RouterHealthCheck";

            SqlConnection conn = new SqlConnection();

                conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlCommand cmd = new SqlCommand(GETIP, conn))
                try
                {
                    SqlDataAdapter a = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    a.Fill(ds);
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            GetList.Add(dr["Primary_IP"].ToString());
                        }                        
                    }
                }
                finally
                {
                    conn.Close();
                }
            return GetList;
        }                                       
      }

【问题讨论】:

  • 你的方法调用顺序是什么?我认为您应该将所有三个功能合二为一并准备最终的 DataTable,然后再将其分配为 GridView 的 DataSource
  • protected void Page_Load(object sender, EventArgs e) { PullData();获取列表(); }
  • 显然,当我调用 PingIT 时,网格视图会获取一个新的数据源并隐藏已经存在的数据
  • 一方面,您可以将结果字段添加到选择列表作为硬编码值 - 这将使代码更易于阅读:Select 'Offline' as Results, RX_ID, Shop_id.... 但随后您需要找到并更新适当的基于某些标准的行。或者您可以将 ping 数据放入第二个数据表和 Merge the tables
  • 我之前尝试过合并,但在尝试将数组列表添加到当前数据表时遇到了麻烦。能给我一个示例代码吗?

标签: c# asp.net gridview arraylist


【解决方案1】:

根据您代码中的 SQL 查询,我认为您可以拥有这个

protected void Page_Load(object sender, EventArgs e)
        {
            PullData();
        }
        public void PullData()
        {            
            string SQLRET = "SELECT RX_ID, ShopID, Primary_IP, ServiceType, Hardware FROM RouterHealthCheck";

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            conn.Open();

            SqlDataAdapter da = new SqlDataAdapter(SQLRET, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dt.Columns.Add("Results", typeof(string));

//iterate the rows and ping each IP. Additionaly you can also code to ignore repeating IP
foreach (DataRow dr in ds.Tables[0].Rows)
                        {                            
Ping ping = new Ping();
                PingReply pingreply = ping.Send(dr["Primary_IP"].ToString());

                if (pingreply.Status == IPStatus.Success)
                {
                    dr["Results"] = string.Format("Success");
                }
                else
                {
                    dr["Results"] = string.Format("Offline");
                }
                        } 

GridViewRouters.DataSource = dt;
                GridViewRouters.DataBind();
}

【讨论】:

  • 解决了谢谢,我不得不将 Foreach 语句更改为:foreach (DataRow dr in dt.Columns[0].Table.Rows) 但看起来一切正常!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 2013-07-25
相关资源
最近更新 更多