如何在不使用 IEnumerator 的情况下通过 while 循环从数据集中的 table 获取偶数列

How to get even columns from table in dataset through while loop without using IEnumerator

我只想通过不使用 ienumerators 的 while 循环来获取偶数列。 while 循环必须是索引格式。

代码如下:

protected void Page_Load(object sender, EventArgs e)
        {

            string query = "SELECT * FROM Notifications_ WHERE News_Status='1'";

            connection  obj_con= new connection();


            SqlCommand myCommand = new SqlCommand(query);

            DataSet ds = obj_con.executeQueryDataSet(myCommand);

            String temp_data = "<table> <tr><h2 align='center' style='background-color:#00254c; color:white; vertical-align:top'>Highlights</h2></tr><tr><td> </td></tr><tr><td> </td><td> </td></tr><tr><td></td><td> </td></tr>";



            foreach (DataTable table in ds.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    temp_data = temp_data + "<tr></tr><tr><td style='background-color:gray; color:white'><b>" + row["News_Day"] + "</b></td></tr><tr><td style='background-color:#f6f6f6; color:#00254c'><h2 align='center'>" + row["News_date"] + "</h2></td><td style='color:#00254c'><b>" + row["News_title"] + "</b></td></tr><tr><td></td><td><a href=" + row["News_Link"] + " style='color:GrayText; text-decoration:none'>" + row["News_Desc"] + "</td></tr>";
                }
            }

            this.notification_data.InnerHtml = temp_data + "</table>";
        }

您可以按照示例代码所示进行尝试。

string[] columnNames = (from dc in table.Columns.Cast<DataColumn>()
                        select dc.ColumnName).ToArray();


do
    {
       if (i%2==0)
           //do something here
        i++;
    } while (i < columnNames.Length);