How can I put a checkbox in a column of my DataGrid?
来源:互联网 发布:中国人口迁移数据 编辑:程序博客网 时间:2024/05/16 06:58
You create a custom DataTableStyle that contains column styles for each column you want to display. You add the column styles in the order you want them to appear. Here are the steps to add an string column, an int column and a bool check column to a DataGrid. You can also download a working project.
// code assumes you have a DataSet named myDataSet, a table named "EastCoastSales" and a DataGrid myDataGrid//STEP 1: Create a DataTable style object and set properties if required.
DataGridTableStyle ts1 = new DataGridTableStyle();
//specify the table from dataset (required step)
ts1.MappingName = "EastCoastSales";
// Set other properties (optional step)
ts1.AlternatingBackColor = Color.LightBlue;
//STEP 2: Create a string column and add it to the tablestyle
DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName"; //from dataset table
TextCol.HeaderText = "Customer Name";
TextCol.Width = 250;
ts1.GridColumnStyles.Add(TextCol);
//STEP 3: Create an int column style and add it to the tablestyle
//this requires setting the format for the column through its property descriptor
PropertyDescriptorCollection pdc = this.BindingContext
[myDataSet, "EastCoastSales"].GetItemProperties();
//now created a formated column using the pdc
DataGridDigitsTextBoxColumn csIDInt =
new DataGridDigitsTextBoxColumn(pdc["CustID"], "i", true);
csIDInt.MappingName = "CustID";
csIDInt.HeaderText = "CustID";
csIDInt.Width = 100;
ts1.GridColumnStyles.Add(csIDInt);
//STEP 4: Add the checkbox
DataGridColumnStyle boolCol = new DataGridBoolColumn();
boolCol.MappingName = "Current";
boolCol.HeaderText = "Info Current";
//uncomment this line to get a two-state checkbox
//((DataGridBoolColumn)boolCol).AllowNull = false;
boolCol.Width = 150;
ts1.GridColumnStyles.Add(boolCol);
//STEP 5: Add the tablestyle to your datagrid's tablestlye collection
myDataGrid.TableStyles.Add(ts1);
5.16 How can I restrict the keystrokes that will be accepted in a column of my datagrid?
You can create a custom column style and handle the KeyPress event of its TextBox member. Below is the code showing how this might be done. You can also download a sample project (C#, VB) that shows an implementation of this idea.
public class DataGridDigitsTextBoxColumn : DataGridTextBoxColumn{
public DataGridDigitsTextBoxColumn(System.ComponentModel.PropertyDescriptor pd, string format, bool b)
: base(pd, format, b)
{
this.TextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(HandleKeyPress);
}
private void HandleKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//ignore if not digit or control key
if(!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
e.Handled = true;
//ignore if more than 3 digits
if(this.TextBox.Text.Length >= 3 && !char.IsControl(e.KeyChar))
e.Handled = true;
}
protected override void Dispose(bool disposing)
{
if(disposing)
this.TextBox.KeyPress -= new System.Windows.Forms.KeyPressEventHandler(HandleKeyPress);
base.Dispose(disposing);
}
}
- How can I put a checkbox in a column of my DataGrid?
- How can I insert a checkbox into the header of my view?
- How Can i Store photo (image) in column of table
- How can I dump only certain entries of a table based on my condition?
- How can I recover a file that I deleted in my local repo from the remote repo in Git?
- I want to do custom handling of special keys such as the Tab key or F2 in the TextBox of a column in the DataGrid. How do I subc
- How can I put a ListView into a ScrollView without it collapsing?
- How do I create a log file of my installation?
- How can i get hold of all of the visible items in my QListView?
- How can a program reboot my PC
- How can a program reboot my PC
- How can I remove all "name" files in all of my subdirectories on Linux
- Can we put a continue in a switch-case statement?
- How do I install a file in my local repository along with a generic POM?
- How to: Encrypt a Column of Data
- How can I determine the byte offset of a field within a structure?
- How can I create a tray icon
- How can I create a tray icon
- 移:该死的java中文问题
- 移:Naked Objects
- 移:使用Struts提交多行数据
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- 移:AOP技术原理
- How can I put a checkbox in a column of my DataGrid?
- 移:Spring AOP的简单例子
- 移:使用Spring AOP实现MVC拦截器
- 移:扩展OGNL
- I want to do custom handling of special keys such as the Tab key or F2 in the TextBox of a column in the DataGrid. How do I subc
- 移:webwork的多行提交
- Web中使用javascript和vbscript访问activex控件
- 移:webwork的bug?
- DOS系统功能调用与BIOS中断调用