C#設(shè)置WinForm中DataGrid列的方法(列寬/列標(biāo)題等)
本文實(shí)例講述了C#設(shè)置WinForm中DataGrid列的方法。分享給大家供大家參考。具體如下:
寫winForm的程序,難免要用DataGrid,自然也就需要設(shè)置列格式啊,標(biāo)題之類的!但是經(jīng)常列標(biāo)題設(shè)置后沒反應(yīng),好惡心!
這幾天做了個(gè)程序,自己研究了一下,主要有有一個(gè)地方要注意!那就是下面代碼中dts.MappingName="Table"; 這段!以下代碼不需要在控件上做任何設(shè)置,照著寫就能搞定!
private void frmLog_Load(object sender, System.EventArgs e) { //設(shè)置DataGrid的列寬 InitDataGridColumnHeader(); //GetResult(); } private void InitDataGridColumnHeader() { DataGridTableStyle dts=new DataGridTableStyle(); //注意:必須加上這一句,否則自定義列格式無法使用 dts.MappingName="Table"; hrgLog.TableStyles.Add(dts); hrgLog.TableStyles[0].GridColumnStyles.Clear(); //========================設(shè)置表頭欄位=========================== DataGridTableStyle dtsLog = new DataGridTableStyle(); DataGridTextBoxColumn colID = new DataGridTextBoxColumn(); colID.Width=80; colID.HeaderText = "記錄序號(hào)"; colID.MappingName = "ID"; hrgLog.TableStyles[0].GridColumnStyles.Add(colID); DataGridTextBoxColumn colLog = new DataGridTextBoxColumn(); colLog.Width=200; colLog.HeaderText = "日志內(nèi)容"; colLog.MappingName = "LogMessage"; hrgLog.TableStyles[0].GridColumnStyles.Add(colLog); DataGridTextBoxColumn colTime = new DataGridTextBoxColumn(); colTime.Width=100; colTime.HeaderText = "記錄時(shí)間"; colTime.MappingName = "LogTime"; hrgLog.TableStyles[0].GridColumnStyles.Add(colTime); DataGridTextBoxColumn colCatalog = new DataGridTextBoxColumn(); colCatalog.Width=100; colCatalog.HeaderText = "日志類別"; colCatalog.MappingName = "LogCatalog"; hrgLog.TableStyles[0].GridColumnStyles.Add(colCatalog); }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù)
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12