您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 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.
本文标题:C#datagridview行列转换
链接地址:https://www.777doc.com/doc-4890990 .html