Regarding the problem of DataGrid, how to make the line width cannot be changed by the user.
(Ie the line width fixation, can not change by dragging) When defining the DataGrid, set the width
// Add record to Customers Table. For (int i = 1; i <4; i ) {newrow1 = tcust.newrow (); newrow1 ["custom"] = (100 * i) .tostring (); tcust.rows .Add (newrow1);} tcust.rows [0] ["CustName"] = "The Wonderful World of Mengxian]"; TCUST.ROWS [1] ["CustName"] = "Net_lover"; tcust.rows [ 2] ["custname"] = "http://xml.sz.luohuedu.net/"; tcust.rows [0] ["custom"] = "Beijing"; tcust.rows [1] ["custom"] = "Shanghai"; tcust.rows [2] ["CustCity"] = "Henan";} private void addressomDataTyleStyle () {DataGridTableSTyle () {dataGridTableStyle Ts1 = new dataGridTableStyle (); ts1.mappingname = "customers"; // Setting the properties TS1. AlternatingBackColor = Color.LightGray; // add Textbox column styles, so that we can capture mouse events DataGridTextBoxColumn textCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custID"; TextCol.HeaderText = "number"; TextCol.Width = 100; // Add event processor TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (textCol); textCol = New DataGridTextBoxColumn (); TextCol.MappingName = "custName"; TextCol.HeaderText = "Name"; TextCol.Width = 100; // add event handlers TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox .DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (textCol); textCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custCity"; TextCol.HeaderText = "address";
TextCol.Width = 100; // add event processor TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (TextCol); dataGrid1.TableStyles .Add (ts1);} protected {if (disposing) {if (component! = Null) {components.dispose ();}} Base.dispose ();} #Region Windows Form Designer generated code private void InitializeComponent () {this.dataGrid1 = new System.Windows.Forms.DataGrid (); this.label1 = new System.Windows.Forms.Label (); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1 .)) BeginInit (); this.SuspendLayout (); // // dataGrid1 // this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Info; this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.WindowText; this. DataGrid1.captionvisible = false; this.DataGrid1 .DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point (11, 9); this.dataGrid1.Name = "dataGrid1"; this. dataGrid1.Size = new System.Drawing.Size (368, 144); this.dataGrid1.TabIndex = 0; this.dataGrid1.MouseDown = new System.Windows.Forms.MouseEventHandler (this.dataGrid1_MouseDown); // // label1 // this.label1.location = new system.drawing.point (4, 166); this.label1.name = "label1"; this.label1.size = new system.drawing.size (383, 23); this. Label1.tabindex = 1;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.label1.Click = new System.EventHandler (this.Form1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size (5 , 13); this.clientsize = new system.drawing.size (387, 201); this.controls.addrange (new system.windows.forms.control [] {this.label1, this.dataGrid1}; this.name = "Form1"; this.text = "Mouse Double-click Event"; (System.comPonentModel.isupportInitialize)). Endinit (); this.ResumeLayout (false);} #ENDREGON [stathread] static Void main () {Application.run (New Form1 ());} Private Void TextBoxDoubleClickHandler (Object Sender, Eventargs E) {MessageBox.show ("Double-click Event. The value of the mouse double-hit:" ((TextBox) Sender) .Text.toString ());} Private void textBoxMousedownHandler (Object sender, MouseEventArgs e) {ix (datet) IME.NOW How to bulk delete datagrid inside information (with the checkBoxDataGridTableStyle ts1 = new DataGridTableStyle (); dataGrid1.DataSource = aTable; // Specify the table from dataset (required step) ts1.MappingName = "A"; // Set other properties (optional step) ts1.AlternatingBackColor = Color.LightBlue; // ts1.AllowSorting = false; ts1.BackColor = Color.Cyan; dataGrid1.TableStyles.Add (ts1); ts1.GridColumnStyles [0] .Width = 200; ts1.DataGrid. Refresh (); Your first question I give an example to you: DataGrid style sheet (DataGridTableStyle) Application ... First we first define a DataTable and a DATAROW Private IDTB_TEMP AS DATATABLE Private idrw_row as datarow private sub getDataTable ) column name idtb_temp.TableName idtb_temp.Columns.Add ( "prdodr_subodr_code") '' 'is defined datatable = "SearchTable" Dim ldcl_header As Windows.Forms.DataGridTextBoxColumn Dim ldgts_styles As New Windows.Forms.DataGridTableStyle ldgts_styles.SelectionForeColor = System.Drawing .Color.yellow '' 'selection of the foreground color of the line, ie font color ldgts_styles.selectionbackcolor = system.drawing.colo R. Brown 'color ldgts_styles.AlternatingBackColor word in the' background color of the selected row ldgts_styles.ForeColor = System.Drawing.Color.Coral '' 'datagrid be displayed = System.Drawing.Color.Cyan' '' datagrid odd the line color displayed ldgts_styles.BackColor = System.Drawing.Color.Cyan '' 'in even rows ldgts_styles.AllowSorting datagrid color displayed = False' '' does not allow these style sheet definition datagrid automatically sorted .. ldgts_styles.MappingName = "SearchTable" LDCL_HEADER = New Windows.Forms.DataGridTextBoxColumn '' 'instantiate a datagridtextboxcolumn ldcl_header.MappingName = "prdodr_subodr_code"' '' defined hereinbefore referenced "column name" ldcl_header.HeaderText = "first column" '' 'shown in column headers in datagrid text ldcl_header.ReadOnly = True '' 'is set to read only those columns ldcl_header.TextBox.BorderStyle = BorderStyle.Fixed3D ldcl_header.TextBox.ForeColor = System.Drawing.Color.Red ldgts_styles.GridColumnStyles.Add (ldcl_header) For i as Integer = 0 To 7 idrw_row = idtb_temp.NewRow idrw_row.Item ( "prdodr_subodr_code") = "of" & i & "OK" idtb_temp.Rows.Add (idrw_row) Next idtb_temp.DefaultView.AllowNew = False Me.DataGrid1.TableStyles.Add ( LDGTS_Styles) Me.DataGrid1.datasource = IDTB_TEMP END SUB third question: Look at my blog Use Checkbox, Combobxo, and DateTimePicker http://blog.9cbs.net/zwxrain/archive/2005/19/258998 in DataGrid. ASPX1. Sets the data source for DataGrid After DataSource, you can add DataGridTableStyle, and then set its AlternatingBackColor property and the BackColor property is alternating row color theme: DataGrid click the middle cell is what triggers the event private void dataGrid1_MouseUp (object sender, System.Windows.Forms.MouseEventArgs e) {? System.Drawing.Point pt = new Point (eX, eY); DataGrid.HitTestInfo hti = dataGrid1.HitTest (pt); if (hti.Type == DataGrid.HitTestType.Cell) {dataGrid1.CurrentCell = new DataGridCell (hti. Row, hti.column; dataGrid1.select (hti.row);}} 1. Sub-page; How do you write next page and page number Click Cypage with the property generator paging? 2. Edit; Edit the implementation. Look at MSDN, look at it, ask you to ask you to think about it. Thank you! ! Page: public void dg_PageIndexChanged (object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) {MyDataGrid.CurrentPageIndex = e.NewPageIndex; BindData ();} Editing: public void edit (object sender, DataGridCommandEventArgs e) {type_dg.EditItemIndex = E.Item.itemindex; binddata ();} public void update (object sender, datagridcommandeventAndeventAndeventAndAndeventAndeventEventEventAndeventEventEventAndeventAndcommandeventAndeventEventEventAndeventEventEventEventAndeventEventEventEventEventAndeventEventEventEventEventEventEventEventEventEventEventEventEventEventEvent) E.Item.cells [0] .controls [0]; TextBox name = (TextBox) E .ITEM.Cells [1] .controls [0]; TextBox Num = (TextBox) E.Item.cells [2] .controls [0]; string Update_oledb = "Update Blog_Type Set B_TYPE_NAME = '" Name.Text "' , b_type_num = '" Num.Text "' where id = '" id.text "' "; OLEDBConnection myconn = new oledbconnection (Application [" strconn "]. Tostring ()); myconn.open (); oledbcommand update_com = new OleDbCommand (update_OleDb, myconn); update_com.ExecuteNonQuery (); type_dg.EditItemIndex = -1; myconn.Close (); BindData ();} public void cancel (object sender, DataGridCommandEventArgs e) {type_dg.EditItemIndex = -1 Binddata ();} Delete: Public Void Delete (Object Sender, DataGridCommandeventArg SE) {string no = type_dg.items [E.Item.itemindex] .cells [0] .text; oledbconnection myconn = new oledbconnection (Application ["strconn"]. toString ()); myconn.open (); string deleteoledb = "delete from blog_type where id =" no; oledbcommand deletecom = new oledbcommand (deleteunicb, myconn); deletecom.executenonQuery (); myconn.close (); binddata ();} How to change the title in WinForm's DataGrid And set the column. Table: ID Name STRDATE ENDDATE 1 A 2004/2/1 2005/2 Requirements The name of the name will be displayed overdue (yes / no) A 2004/2/1 2005/2/1 overdue 365 days thank you! private void Form1_Load (object sender, System.EventArgs e) {SqlConnection CS = new SqlConnection ( "server = mengxianhui; database = SqlPUB2; User Id = sa; password ="); SqlDataAdapter myCommand = new SqlDataAdapter ( "Select lastmodified, objectId from BaseObject ", CS); DataSet myDataSet = new DataSet (); myCommand.Fill (myDataSet," BaseObject "); this.dataGrid1.DataSource = myDataSet.Tables [0]; // set of columns DataGridTextBoxColumn c1 DataGrid = new DataGridTextBoxColumn (); DataGridTextBoxColumn c2 = new DataGridTextBoxColumn (); c1.MappingName = "lastmodified"; c2.MappingName = "objectId"; c1.HeaderText = "hello time [,] Hawaii"; c2.HeaderText = "label"; c1 .Format = "yyyy Year MM month dd day"; c1.Width = 200; DataGridTableStyle dts = new DataGridTableStyle (); dts.GridColumnStyles.Add (c1); dts.GridColumnStyles.Add (c2); dts.MappingName = "BaseObject "; This.DataGrid1.tablestyles.add (dts);} Topic: DataGrid checkboxlist application problem You can add a template column, then edit the template column, join a Checkbox and a checkboxlist, then name the two controls, CODE Join Checkbox1_Checkedchanged event, put Checkbox's auto postb ACK is set to True, then write the feature you want to implement at the checkbox1_checkedchanged event. Here is a simple example, delete DataGrid current row data: (the front data has been bound to DataGrid1) string strsql = "delete from table1 where id = @ id"; string text = dataGrid1 [dataGrid1.currentcell.RowNumber, 0]. ToString (); testDataSet1.Table1.Rows [dataGrid1.CurrentCell.RowNumber] .Delete (); testDataSet1.AcceptChanges (); sqlDataAdapter2.DeleteCommand.Parameters [ "@ Id"] Value = text;. sqlDataAdapter2.DeleteCommand.Connection.Open (); sqldataadapter2.deleteCommand.executenonQuery (); sqldataadapter2.deleteCommand.connection.Close (); Topic: How to generate adaptive columns in DataGridView? Adaptive column width: 'dategrid column width control function Public Sub SizeColumnsToContent (ByVal dataGrid As DataGrid, ByVal nRowsToScan As Integer) Dim Graphics As Graphics = dataGrid.CreateGraphics () Dim tableStyle As DataGridTableStyle = New DataGridTableStyle Try Dim dataTable As DataTable = CType ( dataGrid.DataSource, DataTable) If -1 = nRowsToScan Then nRowsToScan = dataTable.Rows.Count Else nRowsToScan = System.Math.Min (nRowsToScan, dataTable.Rows.Count) End If dataGrid.TableStyles.Clear () tableStyle.MappingName = dataTable .TableName Dim columnStyle As DataGridTextBoxColumn Dim iWidth As Integer For iCurrCol As Integer = 0 To dataTable.Columns.Count - 1 Dim dataColumn As DataColumn = dataTable.Columns (iCurrCol) columnStyle = New DataGridTextBoxColumn columnStyle.TextBox.Enabled = True columnStyle.HeaderTe Xt = datacolumn.columnname columnStyle.mappingname = dataColumn.ColumnName iWidth = CInt (Graphics.MeasureString (columnStyle.HeaderText, dataGrid.Font) .Width) Dim dataRow As DataRow For iRow As Integer = 0 To nRowsToScan - 1 dataRow = dataTable.Rows (iRow) If dataRow (dataColumn.ColumnName ) <> Nothing Then Dim iColWidth As Integer = CInt (Graphics.MeasureString (dataRow.ItemArray (iCurrCol) .ToString (), dataGrid.Font) .Width) Dim iColHight As Integer = CInt (Graphics.MeasureString (dataRow.ItemArray (iCurrCol ) .ToString (), dataGrid.Font) .Height) iWidth = CInt (System.Math.Max (iWidth, iColWidth)) End If Next columnStyle.Width = iWidth 10 tableStyle.GridColumnStyles.Add (columnStyle) Next dataGrid.TableStyles .Add (TableStyle) catch exception messagebox.show (ex.Message) Finally Graphics.dispose () End Try End Sub Topic: Hidden in DataGrid in WinForm Is there a simple way? I also use DataGrid.TablesTyles [Table] .vercolumnstyles [0] .width = 0; doing this. It should be no other way. Master: How to capture DataGrid's double-click event in WinForm? namespace DataGridDoubleClick {using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1: System.Windows.Forms.Form {private System.Windows. Forms.DataGrid DataGrid1; Private DataSet MyDataSet; DateTime GridMouseDowntime; Private System.Windows.Forms.Label Label1; Private System.comPonentmodel.Container Components = NULL; Public form1 () {INITIALIZECMOMPONENT (); gridMousedowntime = DateTime.now; setup ();} private void setup () {// creates DataSet MakeDataSet () with 2 Table and 1 and Relation DataSet MakeDataSet (); // Data Binding DataGrid1.SetDataBinding (MyDataSet, "CUSTOMERS"); // Add Style AddcustomDataTableStyle ();} Private Void MakeDataSet () {// Create DataSet. MyDataSet = New Dataset ("MyDataSet"); // Create 2 DataTables. DataTable Tcust = New DataTable ("Customers"); // Create two columns and add to the first table Datacolumn CCustid = New Datacolumn ("CustID"); Datacolumn CcustName = New Datacolumn ("CustName"); Datacolumn Ccurrent = New Datacolumn ("CustCity) "); Tcust.columns.add (ccust); tcust.columns.add (ccustname); tcust.columns.add (ccurrent); // Add tables to DataSet. MyDataSet.Tables.Add (TCUST); / * Calculation Tables. Create a DataRow variable * / DATAROW NEWROW1; / / Add record to Customers Table. for (int i = 1; i <4; i ) {newrow1 = tcust.newrow (); newrow1 ["custom" ] = (100 * i) .tostring (); tcust.Rows.add (newrow1);} tcust.row S [0] ["CustName"] = "The Wonderful World of Mengxian]"; TCUST.ROWS [1] ["CustName"] = "Net_Lover"; tcust.rows [2] ["CustName"] = " http://xml.sz.luohuedu.net/ "; tcust.rows [0] [" custom "] =" Beijing "; tcust.rows [1] [" custom "] =" Shanghai "; tcust.rows [ 2] [ "custCity"] = "Henan";} private void AddCustomDataTableStyle () {DataGridTableStyle ts1 = new DataGridTableStyle (); ts1.MappingName = "Customers"; // set the attribute ts1.AlternatingBackColor = Color.LightGray; // Add TEXTBOX column style, So that we can capture mouse events DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custID"; TextCol.HeaderText = "number"; TextCol.Width = 100; // add event handlers TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (textCol); textCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custName"; TextCol.HeaderText = "name"; textCol .Width = 100; // add event processor TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (textCol); textCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custCity"; TextCol.HeaderText = "address"; TextCol.Width = 100; // add event processor TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = New EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (TextCol); dataGrid1.TableStyles.Add (ts1);} protected override void Dispose (bool disposing) {if (disposing) {if (components = null!) {components.Dispose ();}} base .Dispose (disposing);} #region Windows Form Designer generated code private void InitializeComponent () {this.dataGrid1 = new System.Windows.Forms.DataGrid (); this.label1 = new System.Windows.Forms.Label (); (System.comPonentModel.isupportInitialize) (THISDATAGRID1)). BeginInit (); this.suspendlayout (); // // dataGrid1 // this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Info; this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.WindowText; this.dataGrid1.CaptionVisible = false; this.dataGrid1.DataMember = " "; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point (11, 9); this.dataGrid1.Name =" dataGrid1 "; this.dataGrid1.Size = new System.Drawing.Size (368, 144); this.dataGrid1.TabIndex = 0; this.dataGrid1.MouseDown = new System.Windows.Forms.MouseEventHandler (this.dataGrid1_MouseDown); // // label1 // this. Label1.Location = new system.drawing.point (4, 166); this.label1.name = "label1"; this.label1.size = new system.drawing.size (383, 23); this.label1.tabindex = 1; this.label1.textalign = system.drawing.contentalignment.middleCenter; this.label1.click = new system.eventhandler (this.form1_ Click; /// Form1 // this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size; this.Controls.addrange (New System); this.Controls.AddRange .Windows.forms.control [] {this.label1, this.dataGrid1}; this.name = "form1"; this.text = "Sample Double-click Event"; (System.com) (this.). DataGrid1)). Endinit (); this.ResumeLayout (FALSE); } #Endregion [static "static void main () {Application.run (new form1 ());} Private void textBoxDoubleClickHandler (Object Sender, Eventargs E) {MessageBox.show (" Double-click Event. The value of the mouse doubles: " ((TextBox) sender) .Text.ToString ());} private void TextBoxMouseDownHandler (object sender, MouseEventArgs e) {if (DateTime.Now TCUST.ROWS.ADD (new1);} tcust.rows [0] ["custname"] = "[Wonderful World of Meng Xian]"; tcust.rows [1] ["CustName"] = "net_lover"; TCUST .ROWS [2] ["CustName"] = "http://xml.sz.luohuedu.net/"; tcust.rows [0] ["custom"] = "Beijing"; tcust.rows [1] [" custCity "] =" Shanghai "; tCust.Rows [2] [" custCity "] =" Henan ";} private void AddCustomDataTableStyle () {DataGridTableStyle ts1 = new DataGridTableStyle (); ts1.MappingName =" Customers "; // set property ts1.AlternatingBackColor = Color.LightGray; // add Textbox column styles, so that we can capture mouse events DataGridTextBoxColumn textCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custID"; TextCol.HeaderText = "number"; TextCol.Width = 100 ; // add event processor TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (textCol); textCol = new DataGridTextBoxColumn (); textCol . Mappingname = "custname"; Textcol.Headertext = "Name"; textcol.width = 100; // Add event processor TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (TextCol); TextCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custCity"; TextCol.Headertext = "Address"; TextCol.Width = 100; // Add event processor textcol.textbox.mousedown = new mouseeventhandler (TextCol.TextBox.doubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (TextCol); dataGrid1.TableStyles.Add (ts1);} protected override void Dispose (bool disposing) {if (disposing) {if (! components = null) {components.Dispose ();}} base.Dispose (disposing);} #region Windows Form Designer generated code private void InitializeComponent () {this.dataGrid1 = new System.Windows.Forms.DataGrid (); this.label1 = new System.Windows. Forms.Label (); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)) BeginInit ();. this.SuspendLayout (); // // dataGrid1 // this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors. info; this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.WindowText; this.dataGrid1.CaptionVisible = false; this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1 .Location = new system.drawin G.Point (11, 9); this.DataGrid1.name = "datagrid1"; this.dataGrid1.size = new system.drawing.size (368, 144); this.DataGrid1.tabindex = 0; this.DataGrid1.mousedown = new system.windows.Forms.MouseEventHandler (this.DataGrid1_mousedown); /// Label1 // this.label1.location = new system.drawing.point (4, 166); this.label1.name = "label1" ; this.label1.Size = new System.Drawing.Size (383, 23); this.label1.TabIndex = 1; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.label1.Click = new System . EventHandler (this.form1_click); // // form1 // this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (387, 201); this.Controls.Addrange (New System.Windows. Forms.control [] {this.label1, this.dataGrid1}; this.name = "form1"; this.text = "Sample Double-click Event"; ((System.comPonentModel.isupportInitialize) (this.DataGrid1)) .EndInit (); this.ResumeLayout (false);} #endregion [STAThread] static void Main () {Application.Run (new Form1 ());} private void TextBoxDoubleClickHandler (object sender, EventArgs e) {MessageBox.Show ( "Double-double click event occurs the value to which:." ((TextBox) sender) .Text.ToString ());} private void TextBoxMouseDownHandler (object sender, MouseEventArgs e) {if (DateTime.Now /// summary> private void InitializeComponent () {this.dataGrid1 = new System.Windows.Forms.DataGrid (); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)) BeginInit ();. This.SuspendLayout (); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point (8, 56) ; this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size (536, 296); this.dataGrid1.TabIndex = 0; this.dataGrid1.Navigate = new System.Windows.Forms .NavigateEventHandler (this.dataGrid1_Navigate); // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size (6, 14); this.ClientSize = new System.Drawing.Size (560, 389); this.Controls .Add (this.DataGrid1); this.name = "form2"; this.text = "form2"; this.Load = new system.EventHandler (this.ComponentModel.isupportInitialize) (this. DataGrid1)). Endinit (); this.ResumeLayout (false);} # endregionprivate void formatGridcells (Object Sender, DataGri DFORMATCELLENTARGS E) {// color row 1 red (E.Row == 0) E.BackBrush = brushes.red; if (E.Row == 1) E.BackBrush = brushes.red; if (E.Row == 2) E.BackBrush = brushes.red; if (E.Row == 3) E.BackBrush = brushes.red; // color column 4 blue // if (E.COLUMN == 4) // E.BackBrush = Brushes.blue; // set font of some cells to bold // if ((E.ROW E.COLUMN)% 5 == 0) // E.TextFont = New Font (E.TextFont.name, E.TextFont .Size, fontstyle.bold; // set textcolor of some cells to blue // IF ((E.ROW E.COLUMN)% 8 == 0) // E.Forebrush = Brushes.dodgerblue; // set font of some cells to bold, underline, italic with white text on green background // == 0) // {// e.TextFont = New font e.TextFont.name, E.TextFont.Size, FontStyle.Bold | FontStyle.italic | fontstyle.underline); // E.Forebrush = brushes.White; // E.BackBrush = brushes.green; //}} private DataTable SomeDataTable () {dataable dt = new data ("myTable"); // add some columnint ncols = 10; for (int J = 0; j } Public delegate void FormatCellEventHandler (object sender, DataGridFormatCellEventArgs e); public class DataGridFormatCellEventArgs: EventArgs {private int _column; private int _row; private Font _font; private Brush _backBrush; private Brush _foreBrush; private bool _useBaseClassDrawing; public DataGridFormatCellEventArgs (int row, int col, Font font1, Brush backBrush, Brush foreBrush) {_ row = row; _column = col; _font = font1; _backBrush = backBrush; _foreBrush = foreBrush; _useBaseClassDrawing = false;} public int Column {get {return _column;} set {_column = value;}} public int Row {get {return _row;} set {_row = value;}} public Font TextFont {get {return _font;} set {_font = value;}} public Brush BackBrush {get {return _backBrush; } set {_backBrush = value;}} public Brush ForeBrush {get {return _foreBrush;} set {_foreBrush = value;}} public bool UseBaseClassDrawing {get {return _useBaseClassDrawing;} set {_useBaseClassDrawing = value;}}} public class DataGridFormattableTextBoxColumn: DataGridTextBoxColumn {// in yo ur handler, set the EnableValue to true or false, depending upon the row & colpublic event FormatCellEventHandler SetCellFormat; private int _col; public DataGridFormattableTextBoxColumn (int col) {_ col = col;} protected override void Paint (System.Drawing.Graphics g, System .Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) {DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs (rowNum, this._col, this. DataGridtablestyle.DataGrid.font, backbrush, forebrush; if (setcellformat! = Null) {setcellformat (this, e); } If (e.UseBaseClassDrawing) base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); else {g.FillRectangle (e.BackBrush, bounds); g.DrawString (this.GetColumnValueAtRow (source, rowNum ) .Tostring (), e.TextFont, E.Forebrush, Bounds.x, bounds.y);} if (e.textFont! = This.dataGridtablestyle.DataGrid.font) E.TEXTFONT.Dispose ();} protected override void Edit (System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {// comment to make cells unable to become editablebase.Edit (source, rowNum, bounds , Readonly, InstantText, CellisVisible;}}}}}} only needs to change the row in formatGridcells, how to make WinForm's DataGrid controls are read-only properties, some are not read-only attributes 1, manual: dataGrid- > properties -> TableStyles-> GridCoumnStyles2, Code: this.dataGrid1.TableStyles [ "tablename"] GridColumnStyles [ "ID"] ReadOnly = true; this.dataGrid1.TableStyles [ "tablename"] GridColumnStyles [ "ID"]... .Readonly = true; display and hide the column <% @ page language = "vb" autoeventwireup = "false" code "code" code "code" code "code" code " S.ASPX.vb "inherits =" aspxweb.showhidecols "%> Body ms_positioning = "gridLayout"> Imports System.DataImports System.Data.OleDbPublic Class ShowHideCols Inherits System.Web.UI.Page Protected WithEvents btnShow As System.Web.UI.WebControls.Button Protected WithEvents btnHide As System.Web.UI.WebControls.Button Protected WithEvents dtgCusts As System .Web.ui.WebControls.DataGrid # Region "Code" Web Form Designer Generated "'This call is required for the web form designer. InitializeComponent () End Sub # End Region Private Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'where user code page initialization placed btnShow.Text = "display column" btnHide .Text = "Hidden Columns" DTGCUSTS.COLUMNS (1) .Headertext = "" DTGCUSTS.COLUMNS (0) .Headertext = "Title" dTGCUSTS.COLUMNS (2) .Headertext = "release date" DTGCUSTS.COLUMNS (3). HeaderText = "edit" If Not IsPostBack Then BindTheData () End If End Sub Sub BindTheData () Dim objConn As OleDbConnection Dim objCmd As OleDbCommand objConn = New OleDbConnection ( "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" _ Server.MapPath ( "Test.mdb")) Dim strSql As String strSql = "SELECT Top 10 id, Title, CreateDate FROM Document" objCmd = New OleDbCommand (strSql, objConn) objConn.Open () dtgCusts.DataSource = objCmd.ExecuteReader () DTGCUSTS.DATABIND () Objconn.close () Objconn.dispose () End Sub SHOWDETAILS (Byval E AS System.EventArgs) DIM INTCOUN ter As Integer For intCounter = 1 To dtgCusts.Columns.Count - 1 dtgCusts.Columns (intCounter) .Visible = True Next End Sub Sub HideDetails (ByVal sender As System.Object, ByVal e As System.EventArgs) Dim intCounter As Integer For INTCOUNTER = 1 to DTGCUSTS.COLUMNS.COUNT - 1 DTGCUSTS.COLUMNS (INTCOUNTER) .Visible = false Next End Subend Class Topic: How to generate adaptive columns in DataGridView? You can implement: Private Void GridView1_mousemove (Object sender, system.windows.Forms.MouseEventArgs e) {int a = ex / colkeywords.width; int b = (E.Y 10) / Colkeywords.width-3 ; if (a> =