简体   繁体   English

如何使用数据库中的数据手动将列标题设置为datagridview

[英]How to manually set column header to datagridview with data from database

hope you can help me with this. 希望你能帮我这个忙。 Currently, I am working on a dynamic payroll system for our thesis which main function is to create multiple payroll format for different company. 目前,我正在为我们的论文开发一个动态薪资系统,其主要功能是为不同公司创建多种薪资格式。 I can create simple datagridview which shows basic employee information such as Employee's "Full name" and "initial rate" but is it possible to add new header/s using a button function(not hardcoded) which will hold a value just in case the user would like to add deductibles 我可以创建一个简单的datagridview来显示基本的员工信息,例如员工的“全名”和“初始比率”,但是可以使用按钮功能(未硬编码)添加新的标头,该功能将保留一个值,以防万一用户想添加免赔额

You are not specified platform. 您未指定平台。

In case of WinForms : 如果是WinForms

  1. Add unbound column by click or whatever: 通过单击或任何其他方式添加未绑定的列:

      var column = new DataGridViewColumn(new DataGridViewTextBoxCell()); column.Name = "Calculated Column"; column.HeaderText = "Calculated Column"; dataGridView1.Columns.Add(column); 
  2. Add processing of datagridview.cellformatting 添加处理datagridview.cellformatting

     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].Name == "Calculated Column") { var data = (DataObj)dataGridView1.Rows[e.RowIndex].DataBoundItem; e.Value = GetCalculatedColumnValue(data); e.FormattingApplied = false; } } 
  3. Add processing of entered data in column 在列中添加对输入数据的处理

     private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].Name == "Calculated Column") { var data = (DataObj)dataGridView1.Rows[e.RowIndex].DataBoundItem; SetCalculatedColumnValue(data, e.Value); } } 

PS I my case I use DataObj as data bounded object PS我的情况下,我使用DataObj作为数据绑定对象

public class DataObj
{
    public float Price { get; set; }
    public int Amount { get; set; }
}

In case you need only to show external data (read only column) (p3 is not needed) 如果只需要显示外部数据(只读列)(不需要p3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM