C#实现火车时刻表Web Service

Lately I‘m learning the concept of web service using C# programming language.Today let me create a window about train time as follows.

My exercising environment is VS2012, which has been installed on my computer. Of course, vs2010 should have no problem.


1. create a win32 form application



the gray part of the above picture represents the control DataGridView.

2. right click on the project and choose the "add web reference". Then add the following web service link, and give a custom namespace. 



3. Double click the button on the form and enter its event handler function

private void button1_Click(object sender, EventArgs e)
        {          
            if (dataGridView2.Visible == false)
                dataGridView2.Visible = true;
            string[] returnValue = new string[] { "车次", "始发站", "终点站", "发车站",
                "发车时间", "到达站", "到达时间", "里程", "历时" };//中文列名
            string startStation = textBox1.Text;
            string arriveStation = textBox2.Text;

            MyTrainTimeService.TrainTimeWebServiceSoapClient client = new MyTrainTimeService.TrainTimeWebServiceSoapClient();
            DataSet set = client.getStationAndTimeByStationName(startStation, arriveStation, "");
            DataTable t = set.Tables[0];        

            // 更改列名
            for (int i = 0; i < 9; i++)
            {
                t.Columns[i].ColumnName = returnValue[i];
            }

            // 增加序号列,并把它调到第一列
            t.Columns.Add("num");
            t.Columns["num"].SetOrdinal(0);
            for (int i = 0; i < t.Rows.Count; i++)
            {
                t.Rows[i]["num"] = i+1;
            }
            //dataGridView2.AllowUserToOrderColumns = true;
            dataGridView2.AllowUserToAddRows = false;
            dataGridView2.DataSource = t;
        }             
    }
}

OK. So far, everything works fine. let‘s run it with ctrl+F5. Then enter the startstation and endstation, you‘ll see the results.(You should wait for a couple of seconds before the result shows up)



Of course, there are still many imperfections about the application. We should add the validation check of the TextBox, for example. Maybe we can beautify the window a little more. Here I decide to stop, for the time given to me is really short. Improve it when more free time is avaiable.


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。