在DataGridView中设置所有列运行时的默认值
我在datagridview上添加了n个列。 列数不固定,列的名称也是固定的。 另外,我想为每行中的所有列添加deault值
这些列也在运行时添加,gird的数据源也与类相关联
DbDataEntities db = new DbDataEntities(); var MasterAttendanceTypesDetail = db.MasterAttendanceTypes.ToList(); foreach (string AttendanceType in MasterAttendanceTypesDetail.Select(s => s.AttendanceTypeName).ToList()) { if (!dgv1.Columns.Contains(AttendanceType)) { DataGridViewColumn dgchkCol = new DataGridViewColumn(); dgchkCol.Name = AttendanceType; dgv1.Columns.Add(dgchkCol); } }
网格中添加的列数取决于tabel中的条目数几乎为100。
因此,我如何设置每列的默认值。
您可以inheritanceDataGridViewColumn和DataGridViewCell并覆盖DefaultNewRowValue属性。
public class ExtendedColumn : DataGridViewColumn { public ExtendedColumn() : base(new ExtendedCell()) { } } public class ExtendedCell : DataGridViewTextBoxCell { public ExtendedCell() : base() { } public override object DefaultNewRowValue { get { return "aaa"; } } }
并在您的代码中使用它,如下所示:
ExtendedColumn col = new ExtendedColumn(); col.Name = AttendanceType; dgv1.Columns.Add(col);
以下是MSDN的链接,演示了如何使用DefaultNewRowValue:
但由于您可以拥有许多需要默认值的列,因此您可以处理DefaultValuesNeeded事件。
dataGridView1.DefaultValuesNeeded +=new DataGridViewRowEventHandler(dataGridView1_DefaultValuesNeeded); private void dataGridView1_DefaultValuesNeeded(object sender, System.Windows.Forms.DataGridViewRowEventArgs e) { e.Row.Cells[0].Value = "aaa"; e.Row.Cells[1].Value = "bbb"; e.Row.Cells[2].Value = "ccc"; }
如果数据尚未绑定到DataSet,这将是我的方法。 将数据绑定到表允许您在表本身上创建约束和其他规则,而不是弄乱网格控件。
此外,它还允许您在数据输入完成后轻松将数据导出到任何位置。 您可以根据需要将数据传递给GetTable
函数。 我刚刚将样本数据构建成二维数组用于演示。
我强烈建议您考虑以编程方式向表中添加行。 该控件旨在与ADO.NET一起使用。
private void BuildDataGrid() { //-------------------------------------------------- // Create list of test column names and default // values //-------------------------------------------------- string[,] columns = new string[50,2]; for (int n = 0; n < 50; n++) { columns[n, 0] = "column_" + n.ToString(); columns[n, 1] = "def" + n.ToString(); } //-------------------------------------------------- // Bind table to grid //-------------------------------------------------- m_dataGridView.DataSource = GetTable(columns); } /// /// Create a table from the raw data. /// private DataTable GetTable(string[,] columns) { DataTable table = new DataTable(); for (int n = 0; n < columns.GetLength(0); n++) { DataColumn column = new DataColumn(columns[n, 0], typeof(string)) { DefaultValue = columns[n, 1] }; table.Columns.Add(column); } return table; }
手动添加列
for (int i = 0; i <= 5; i++) { dataGridView1.Columns.Add("Column" + i, "Column"+i); }
手动添加行
dataGridView1.Rows.Add(10);
在Datagridview中手动添加值
for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++) { for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++) { dataGridView1.Rows[j].Cells[i].Value = "aaa"; } }
创建从DataGridViewCell提供的新类。 覆盖DefaultNewRowValue以返回所需的默认值(可以将其作为构造函数参数传递)。 在创建列集dgchkCol.CellTemplate到自定义DataGridViewCell实现的新实例之后。
获取GridView计数然后迭代:
for (int i = 0; i <=DS.Tables[0].Rows.Count;; i++) { row[i].Cell[0].FindControl("txtTextBox1").Value="aaa"; row[i].Cell[1].FindControl("txtTextBox2").Value="bbb"; row[i].Cell[2].FindControl("txtTextBox3").Value="ccc"; }
如果datagrid绑定到Entity Framework实体的集合,则可以在实体级别进行初始化。
您可以通过创建部分类并为实体声明构造函数来完成此操作。
例如,如果我有一个实体tblUser
partial class tblUser { public tblUser() { Surname = "Smith"; } } class Program { static void Main(string[] args) { TestEntities test = new TestEntities(); var user = test.tblUsers.CreateObject(); Console.WriteLine(user.Surname); // Prints Smith } }