FAQ for DataGrid

xiaoxiao2021-03-06  44

When we just get in touch with DataGrid, it may be less familiar to it. Sometimes some common settings will make you worried for a long time. In view of this, I summed up its more common usage, I hope to give some people who just contact DataGrid. help. One pair of settings are made, and we have to have certain awareness of the columns of DataGrid, which can be divided into two, DataGridTextBoxColumn and DataGridBoolColumn. Of course, you can also add your own column style, just inherit the datagridcolumnstyle, you will have to override some methods). Below is a code: DataGridTableStyle Style = New DataGridTableStyle ();

Style.mappingname = ds.tables [0] .tablename.toString ();

DataGridBoolcolumn colu = new datagridBoolcolumn ();

COLU.MAPPINGNAME = "isselect"; // Map to a list of tables

Colu.Headertext = ""; // list of head name

COLU.TRUEVALUE = "y"; // The value of the tick

COLU.FALSEVALUE = "N";

COLU.WIDTH = 40; // Set the width of the collected

COLU.Allownull = false; // Does not allow NULL values

COLU.Readonly = true; // Set the collapsed to read only

Style.GridColumnStyles.Add (Colu);

The above code is to create a DataGridBoolColumn column, which is a checkbox column, Checkbox has three states, true, false, null, you can set up colu.allownull = false; cancel the third state.

Look at a code, it is created a DataGridTextBox column

DataGridTextBoxColumn COL;

COL = New DataGridTextBoxColumn ();

Col.mappingname = "DMINIMUMPV";

Col.Headertext = "Minimum Accumulated Performance";

Col.alignment = HorizontalaLAlignment.right; // The alignment of this column

Col.format = "#, ###. 00"; // Set the format displayed

COL.WIDTH = 90;

Style.GridColumnStyles.Add (col);

I believe that these two codes should let you have a certain understanding of the settings of DataGrid!

Second capture Double-click event in DataGrid, which is a simple question, but if you only write a program for DataGrid's doubleclicked, you will find a problem, don't respond when you double-click in the DataGrid Cell, this is because in DataGrid There are textbox, so you need to capture the DoubleClicked event of TextBox, but when you finish this step, look at the effect, if you carefully, you will find a problem, when the cursor has not been in a cell, Double-click the cell and find out? No response, clearly double-click, why? This is because DataGrid decomposes this double-click operation, divided into two clicks, the first makes the cell get the cursor, the second is to click the event, so your double click does not respond, so we have to do it. That is to handle this action as double-taking events, so that we have achieved our purpose. So caught a double-click event to do three things. Here is a code. first step:

Private void DGROLE_DOUBLECLICK (Object Sender, System.EventArgs E)

// dgrole is DataGrid

{

this.frmrolerightshow (); // We double-click what you want to do.

}

Step 2:

Private void textbox_doubleclick (Object Sender, System.EventArgs E)

{

THIS.FRMROLERIGHTSHOW ();

}

third step:

DateTime GridMouseDowntime;

Private void DGRole_MouseDown (Object Sender, System.Windows.MouseEventArgs E)

{

GridMouseDowntime = datetime.now; // record time

}

Private void textbox_mousedown (Object Sender, MouseEventArgs E)

{

IF (DateTime.Now

// If you click on the time less than the system's double-click time, we think it is double-click operation.

{

THIS.FRMROLERIGHTSHOW ();

}

}

How do I take a certain value of a cell? One line of code, as follows:

String strrole = DGROLE [ROW, Column] .tostring ();

The above is that I have summed it out by watching the post on 9CBS and in practice. If you have something experience to DataGrid, you may wish to write it out and share it with you.

Author Blog:

http://blog.9cbs.net/rijing2000/

转载请注明原文地址:https://www.9cbs.com/read-79827.html

New Post(0)