(C#)WPF根据设计的数据动态生成火车席位

2017-06-01 08:17:54来源:CSDN作者:GuangXi_YangNan人点击

1、效果图,页面设计

           

 2、代码  //生成座位        DataTable seatdt;        string A = "";        string B = "";        string C = "";        string D = "";        string F = "";                      private void btnCreateSeat_Click(object sender, RoutedEventArgs e)        {            //声明一个table            seatdt = new DataTable();            //获取席位个数            int sumSeat = Convert.ToInt32(txt_sumSeat.Text);            //获取列数            int columns = Convert.ToInt32(cboSeatColumns.Text);            //当显示的是座位席别的DGV的时候            if (gridZuoWei.Visibility==Visibility.Visible)              {                seatdt.Columns.Add("columnA"typeof(string));                seatdt.Columns.Add("columnB"typeof(string));                seatdt.Columns.Add("columnC"typeof(string));                seatdt.Columns.Add("columnD"typeof(string));                seatdt.Columns.Add("columnF"typeof(string));                for (int i = 0; i < sumSeat;)                {                    if (columns == 5)                    {                        i = columns5(i, sumSeat);                    }                    else if (columns == 4)                    {                        i = columns4(i);                    }                    else if (columns == 3)                    {                        i = columns3(i);                    }                    else if (columns == 2)                    {                        i = columns2(i);                    }                    if (i > sumSeat)                    {                        MessageBox.Show("席位个数设置有误!应该为" + i);                        txt_sumSeat.Text = i.ToString();                                           }                }                //绑定数据给席位为座位的DGV                dgv_Seat.ItemsSource = seatdt.AsDataView();              }            else //当显示的是卧铺席别的DGV的时候            {                seatdt.Columns.Add("number"typeof(string));                seatdt.Columns.Add("top"typeof(string));                seatdt.Columns.Add("centen"typeof(string));                seatdt.Columns.Add("bottom"typeof(string));             if (radioButtonTCB.IsChecked==true)  //硬卧、有上、中、下铺                {                    if (sumSeat % 3 == 0)                    {                        for (int i = 0; i < sumSeat / 3;)                        {                            i = WoPu3(i);                        }                    }                    else                    {                        MessageBox.Show("席位个数设置有误!应该为" + (sumSeat + (3 - sumSeat % 3)));                        txt_sumSeat.Text = (sumSeat + (3 - sumSeat % 3)).ToString();                        btnCreateSeat_Click(nullnull); //调用自身开始重新计算                    }                }              else //只有上、下铺             {                    if ((sumSeat % 2) == 0)                    {                        for (int i = 0; i < sumSeat / 2;)                        {                            i = WoPu2(i);                        }                    }                    else                    {                        MessageBox.Show("席位个数设置有误!应该为" + (sumSeat + 1));                        txt_sumSeat.Text = (sumSeat + 1).ToString();                        btnCreateSeat_Click(nullnull); //调用自身开始重新计算                    }                }                                             dgv_WoPuSeat.ItemsSource = seatdt.AsDataView();  //绑定数据给席位为卧铺的DGV          }                                 ShengChengZuoWei = true;        }        #region  生成座位席位编号的方法        //列数为5执行的方法        public int columns5( int i,int sumSeat)        {                if (i == 0)                {                    i++;                    seatdt.Rows.Add("A0" + (i++), "B0" + (i++), """D0" + (i++), "F0" + i);                }                else if (i < sumSeat - 4)                {                    i = colA(i);                    i = colB(i);                    i = colC(i);                    i = colD(i);                    i = colF(i);                    seatdt.Rows.Add(A, B, C, D, F);                }                else                {                  i++;                 //添加一行数据到table中                  seatdt.Rows.Add("A" + (i++), "B" + (i++), """D" + (i++), "F" + i);                }                        return i;        }        private int colA(int i)        {            if (i++ < 9)            {                A = "A0" + i;            }            else            {                A = "A" + i;            }            return i;                }        private int colB(int i)        {            if (i++ < 9)            {                B = "B0" + i;            }            else            {                B = "B" + i;            }            return i;        }        private int colC(int i)        {            if (i++ < 9)            {                C = "C0" + i;            }            else            {                C = "C" + i;            }            return i;        }        private int colD(int i)        {            if (i++ < 9)            {                D = "D0" + i;            }            else            {                D = "D" + i;            }            return i;        }        private int colF(int i)        {            if (i++ < 9)            {                F = "F0" + i;            }            else            {                F = "F" + i;            }            return i;        }        //列数为4执行的方法        public int columns4(int i)        {            i = colA(i);            i = colC(i);            i = colD(i);            i = colF(i);            //添加一行数据到table中            seatdt.Rows.Add(A, "", C, D, F);            return i;        }        //列数为3执行的方法        public int columns3(int i)        {            i = colA(i);            i = colC(i);                    i = colF(i);                      //添加一行数据到table中            seatdt.Rows.Add(A, "", C, "", F);            return i;        }        //列数为2执行的方法        public int columns2(int i)        {            i = colA(i);                       i = colF(i);                     //添加一行数据到table中            seatdt.Rows.Add(A, """""", F);            return i;        }        #endregion        #region 生成卧铺编号的方法        public int WoPu3(int i)        {            if (i++ < 9)            {                A = "0" + i;            }            else            {                A = "" + i;            }            if (i < 10)            {                B = "0" + i+"上铺";            }            else            {                B = i + "上铺";            }            if (i < 10)            {                C = "0" + i + "中铺";            }            else            {                C = i + "中铺";            }            if (i < 10)            {                D = "0" + i + "下铺";            }            else            {                D = i + "下铺";            }            //添加一行数据到table中            seatdt.Rows.Add(A,B,C,D);            return i;        }        public int WoPu2(int i)        {            if (i++ < 9)            {                A = "0" + i;            }            else            {                A = "" + i;            }            if (i < 10)            {                B = "0" + i+"上铺";            }            else            {                B = i + "上铺";            }                   if (i < 10)            {                D = "0" + i+"下铺";            }            else            {                D = i + "下铺";            }            //添加一行数据到table中            seatdt.Rows.Add(A, B, "", D);            return i;        }        #endregion  

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台