C#查询数据库填充表格

        private void BindDataToAlarm()
        {
            //数据库返回预警信息
            DataTable dt = new DataTable();
            dt = selectWarningDataTable();
            //MessageBox.Show("预警信息wmf");
            DataTable dtData = new DataTable();
            dtData.Columns.Add("RiverName", typeof(string));
            dtData.Columns.Add("STCD", typeof(string));
            dtData.Columns.Add("ForecastTM", typeof(string));
            dtData.Columns.Add("MaxQ", typeof(string));
            dtData.Columns.Add("MaxQTM", typeof(string));
            dtData.Columns.Add("FID", typeof(string));
            dtData.Columns.Add("STNM", typeof(string));
            
             List<Model.StationInfo> lstStation = Sunny.RiverForecast.Business.Common.GlobalData.GlobalData_Station;
 
            foreach (DataRow dr in dt.Rows)
            {
                DataRow dr1 = dtData.NewRow();
                var st= lstStation.FirstOrDefault(p => p.STCD == dr["STCD"].ToString());//表达式参数
                if (st != null)
                {
                    dr1[0] = st.RVNM;
                    dr1[6] = st.STNM;
                }
                
                dr1[1] = dr["STCD"].ToString();
                dr1[2] = dr["SForecastTime"].ToString();
                dr1[3] = dr["RQ"].ToString();
                dr1[4] = dr["RQTime"].ToString();
                
                dtData.Rows.Add(dr1);
            } 

            /**
            DataRow dr1 = dtData.NewRow();
            dr1[0] = "新安江";
            dr1[1] = "屯溪";
            dr1[2] = "2013-06-27 20:00";
            dr1[3] = "2877.91";
            dr1[4] = "2013-06-27 23:00";
            dr1[5] = "2";
            dtData.Rows.Add(dr1);

            DataRow dr2 = dtData.NewRow();
            dr2[0] = "秋浦河";
            dr2[1] = "高坦";
            dr2[2] = "2013-06-30 19:00";
            dr2[3] = "1406.89";
            dr2[4] = "2013-06-30 20:00";
            dr2[5] = "26";
            dtData.Rows.Add(dr2);

            DataRow dr3 = dtData.NewRow();
            dr3[0] = "淠河东源";
            dr3[1] = "磨子潭";
            dr3[2] = "2013-07-07 12:00";
            dr3[3] = "1117.1";
            dr3[4] = "2013-07-07 13:00";
            dr3[5] = "35";
            dtData.Rows.Add(dr3);
            */
            this.gcAlarmInfo.DataSource = dtData;
        }

你可能感兴趣的:(数据库)