DataGridView.AutoGenerateColumns 属性
来源:互联网 发布:淘宝店铺首页怎么弄 编辑:程序博客网 时间:2024/06/08 04:06
看GridView控件如何绑定到一个数据源控件的.aspx,看到AutoGenerateColumns=“False”,好奇这个属性的使用,于是查了下msdn的解释。译文翻译的略差,此附原文。
Gets or sets a value indicating whether columns are created automatically when the DataSource or DataMemberproperties are set.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Columns are automatically generated when this property is set to true and the DataSource or DataMember properties are set or changed.Columns can also be automatically generated when the AutoGenerateColumns property is changed from false to true.If this property is true and the DataSource changes so there are columns that do not match the columns of the previous DataSource value, data in the unmatched columns is discarded.This property is ignored if the DataSource or DataMember properties are not set.
When AutoGenerateColumns is set to true, the DataGridView control generates one column for each public property of the objects in the data source.If the bound objects implement the ICustomTypeDescriptor interface, the control generates one column for each property returned by the GetProperties method.Each column header will contain the value of the property name the column represents.
If you set the DataSource property but set AutoGenerateColumns to false, you must add columns manually.You can bind each added column to the data source by setting the DataGridViewColumn.DataPropertyName property to the name of a property exposed by the bound objects.
例子:
public class Form1 : Form{ private List<Employee> employees = new List<Employee>(); private List<Task> tasks = new List<Task>(); private Button reportButton = new Button(); private DataGridView dataGridView1 = new DataGridView(); [STAThread] public static void Main() { Application.Run(new Form1()); } public Form1() { dataGridView1.Dock = DockStyle.Fill; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; reportButton.Text = "Generate Report"; reportButton.Dock = DockStyle.Top; reportButton.Click += new EventHandler(reportButton_Click); Controls.Add(dataGridView1); Controls.Add(reportButton); Load += new EventHandler(Form1_Load); Text = "DataGridViewComboBoxColumn Demo"; } // Initializes the data source and populates the DataGridView control. private void Form1_Load(object sender, EventArgs e) { PopulateLists(); dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = tasks; AddColumns(); } // Populates the employees and tasks lists. private void PopulateLists() { employees.Add(new Employee("Harry")); employees.Add(new Employee("Sally")); employees.Add(new Employee("Roy")); employees.Add(new Employee("Pris")); tasks.Add(new Task(1, employees[1])); tasks.Add(new Task(2)); tasks.Add(new Task(3, employees[2])); tasks.Add(new Task(4)); } // Configures columns for the DataGridView control. private void AddColumns() { DataGridViewTextBoxColumn idColumn = new DataGridViewTextBoxColumn(); idColumn.Name = "Task"; idColumn.DataPropertyName = "Id"; idColumn.ReadOnly = true; DataGridViewComboBoxColumn assignedToColumn = new DataGridViewComboBoxColumn(); // Populate the combo box drop-down list with Employee objects. foreach (Employee e in employees) assignedToColumn.Items.Add(e); // Add "unassigned" to the drop-down list and display it for // empty AssignedTo values or when the user presses CTRL+0. assignedToColumn.Items.Add("unassigned"); assignedToColumn.DefaultCellStyle.NullValue = "unassigned"; assignedToColumn.Name = "Assigned To"; assignedToColumn.DataPropertyName = "AssignedTo"; assignedToColumn.AutoComplete = true; assignedToColumn.DisplayMember = "Name"; assignedToColumn.ValueMember = "Self"; // Add a button column. DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); buttonColumn.HeaderText = ""; buttonColumn.Name = "Status Request"; buttonColumn.Text = "Request Status"; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Add(idColumn); dataGridView1.Columns.Add(assignedToColumn); dataGridView1.Columns.Add(buttonColumn); // Add a CellClick handler to handle clicks in the button column. dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); } // Reports on task assignments. private void reportButton_Click(object sender, EventArgs e) { StringBuilder report = new StringBuilder(); foreach (Task t in tasks) { String assignment = t.AssignedTo == null ? "unassigned" : "assigned to " + t.AssignedTo.Name; report.AppendFormat("Task {0} is {1}.", t.Id, assignment); report.Append(Environment.NewLine); } MessageBox.Show(report.ToString(), "Task Assignments"); } // Calls the Employee.RequestStatus method. void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { // Ignore clicks that are not on button cells. if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Status Request"].Index) return; // Retrieve the task ID. Int32 taskID = (Int32)dataGridView1[0, e.RowIndex].Value; // Retrieve the Employee object from the "Assigned To" cell. Employee assignedTo = dataGridView1.Rows[e.RowIndex] .Cells["Assigned To"].Value as Employee; // Request status through the Employee object if present. if (assignedTo != null) { assignedTo.RequestStatus(taskID); } else { MessageBox.Show(String.Format( "Task {0} is unassigned.", taskID), "Status Request"); } }}
- DataGridView.AutoGenerateColumns 属性
- Gridview的AutoGenerateColumns属性
- AutoGenerateColumns的使用属性介绍
- asp:GridView标签学习---AutoGenerateColumns属性
- datagridview 属性
- DataGridView 属性
- DataGridView常用属性 《一》
- DataGridView 属性说明
- 关键字: datagridview 属性 说明
- winform DataGridView 属性说明
- DataGridView的属性1
- DataGridView的属性2
- DataGridView常用属性
- DataGridView常用属性
- DatagridView 属性,功能 总结
- DataGridView属性、方法、事件
- winform DataGridView 属性说明
- DataGridView相关-自定义属性?
- iOS设计模式----委托模式
- 普通JAVA类 如何获取,WEB项目的根路径_百度知道
- 计算机科学中最重要的32个算法
- J2SE常用包的含义
- HTML5 技术站点
- DataGridView.AutoGenerateColumns 属性
- Uva-11995-I Can Guess the Data Structure!
- 黑马程序员 笔记(九)——面向对象(内部类)
- YII 返回视图3种方式
- Oracle 使用RMAN COPY 移动 整个数据库 位置 示例
- Activity 启动模式
- indy组件在2010的重装
- jquery读取json格式
- 我来理解extern "C"