C#datagridview行列转换

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

DisplayingVerticalRowsinDataGridViewIntroductionThisarticleisjustanexampleofNeerajJain'sworkinDisplayingVerticalRowsinaDataGrid.BackgroundHereIdesignedaC#applicationwithaDataGridViewcontainingsomeinformationfromaDataTableobjectinthenormalmode(HorizontalRows).WhenyoupressonFlipDatagridebutton,thedatagriddisplaysthesametablebutintheFlippedmode(VerticalRows).UsingtheCode1.CreateanewC#.NET2005WindowsApplication.2.AddthebuttonsandtheDataGridViewobjectontheFormasshowninthepicture.3.DeclaretheDataSetandDataTableobjectasfollows:4.publicpartialclassForm1:Form5.{6.DataSetds=null;7.DataTabledt=null;8.9.publicForm1()10.{11.InitializeComponent();}12.CreateanewmethodthatcreatesandreturnsaDataTableobjectfilledwithsomedata:13.privatestaticDataTableGetCustomers()14.{15.DataTabletable=newDataTable();16.table.TableName=Customers;17.18.table.Columns.Add(Name,typeof(string));19.table.Columns.Add(Price,typeof(string));20.table.Columns.Add(Country,typeof(string));21.22.table.Rows.Add(newobject[]{Mohamad,1700,Egypt});23.table.Rows.Add(newobject[]{Tarek,550,Syria});24.table.Rows.Add(newobject[]{Gamal,762,SaudiArabia});25.26.table.AcceptChanges();27.28.returntable;}29.CreateOnLoadeventandaddthefollowingcode:30.privatevoidForm1_Load(objectsender,EventArgse)31.{32.ds=newDataSet();33.dt=newDataTable();34.35.dt=GetCustomers();36.ds.Tables.Add(dt);37.38.DataViewmy_DataView=ds.Tables[0].DefaultView;39.this.my_DataGrid.DataSource=my_DataView;}40.CreatetheFlipDataSetmethod:41.publicDataSetFlipDataSet(DataSetmy_DataSet)42.{43.DataSetds=newDataSet();44.45.foreach(DataTabledtinmy_DataSet.Tables)46.{47.DataTabletable=newDataTable();48.49.for(inti=0;i=dt.Rows.Count;i++)50.{table.Columns.Add(Convert.ToString(i));}51.52.DataRowr;53.for(intk=0;kdt.Columns.Count;k++)54.{55.r=table.NewRow();56.r[0]=dt.Columns[k].ToString();57.for(intj=1;j=dt.Rows.Count;j++)58.{r[j]=dt.Rows[j-1][k];}59.table.Rows.Add(r);60.}61.ds.Tables.Add(table);62.}63.64.returnds;}65.Addthebuttonclickeventasfollows:66.privatevoidbutFlip_Click(objectsender,EventArgse)67.{68.DataSetnew_ds=FlipDataSet(ds);//FliptheDataSet69.DataViewmy_DataView=new_ds.Tables[0].DefaultView;70.this.my_DataGrid.DataSource=my_DataView;71.72.butFlip.Enabled=false;73.butNormal.Enabled=true;74.}75.76.privatevoidbutNormal_Click(objectsender,EventArgse)77.{78.DataViewmy_DataView=ds.Tables[0].DefaultView;79.this.my_DataGrid.DataSource=my_DataView;80.81.butFlip.Enabled=true;82.butNormal.Enabled=false;83.}84.85.privatevoidbutExit_Click(objectsender,EventArgse)86.{87.Application.Exit();}Nowbuildandruntheapplication.

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功