将新列绑定到GridView C#**更新**

时间:2016-04-14 10:20:48

标签: c# asp.net gridview arraylist

好的更新,因为我今天下午得到了一点......

我已经将我的代码合并在一起,但它仍然不正确,我已经添加了一张图片,让你们了解我想要做什么以及它看起来像什么。

结果将进入新的一排?我想我需要添加一个匹配,所以如果我的数组列表中的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;
        }                                       
      }

whatitlookslike

1 个答案:

答案 0 :(得分: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();
}