如何将一个dataTable列的值除以标签文本的值



问题

我怎么能有专栏";天;填充列"的结果;小时";除以标签25.文本?

代码

下面是我试图实现的代码。我认为这个问题在前臂部分。

dataGridView4.AutoGenerateColumns = false;
DataTable dt2 = new DataTable();
dt2.Columns.Add("Type", typeof(String));
dt2.Columns.Add("Hours", typeof(String));
dt2.Columns.Add("PerDiem", typeof(String));
dt2.Columns.Add("Days", typeof(decimal)).Expression = "[Hours]/" + label25.Text;
DataGridViewComboBoxColumn type = new DataGridViewComboBoxColumn();
var list10 = new List<string>() { "Tom", "Dick", "Harry"};
type.DataSource = list10;
type.HeaderText = "Type";
type.DataPropertyName = "Type";
DataGridViewTextBoxColumn hours = new DataGridViewTextBoxColumn();
hours.HeaderText = "Hours";
hours.DataPropertyName = "Hours";
DataGridViewComboBoxColumn perdiem = new DataGridViewComboBoxColumn();
var list11 = new List<string>() { "YES", "NO"};
perdiem.DataSource = list11;
perdiem.HeaderText = "PerDiem";
perdiem.DataPropertyName = "PerDiem";
DataGridViewTextBoxColumn days = new DataGridViewTextBoxColumn();
days.HeaderText = "Days";
days.DataPropertyName = "Days";
dataGridView4.DataSource = dt2;
dataGridView4.Columns.AddRange(type, hours, perdiem, days);
dataGridView4.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
type.FlatStyle = FlatStyle.Flat;
perdiem.FlatStyle = FlatStyle.Flat;

任何帮助都将不胜感激!!提前谢谢。

更新


DataTable dt2 = new DataTable();
dt2.Columns.Add("Hours", typeof(decimal));
dt2.Columns.Add("HoursInAWorkingDay", typeof(decimal));
dt2.Columns.Add("Days", typeof(decimal)).Expression = "[Hours]/[HoursInAWorkingDay]";
DataGridViewTextBoxColumn hours = new DataGridViewTextBoxColumn();
hours.HeaderText = "Hours";
hours.DataPropertyName = "Hours";
DataGridViewComboBoxColumn perdiem = new DataGridViewComboBoxColumn hoursinaworkingday = new DataGridViewComboBoxColumn();
var list13 = new List<string>() { "8", "10", "12", "14" };
hoursinaworkingday.DataSource = list13;
hoursinaworkingday.HeaderText = "Work Day";
hoursinaworkingday.DataPropertyName = "Work Day";
DataGridViewTextBoxColumn days = new DataGridViewTextBoxColumn();
days.HeaderText = "Days";
days.DataPropertyName = "Days";

做出了建议的改变,但我仍然做不到。不幸的是,在小时中输入数据和/或从小时nAWorkDay中选择选项时,"天"列没有填充值。请帮忙。我知道我们很接近。

更改

dt2.Columns.Add("Days", typeof(String));

dt2.Columns.Add("Days", typeof(decimal)).Expression = "[Hours]/" + label25.Text;

同时使您的小时列类型为(十进制(,并从代码底部删除for循环

"天"列现在是只读的,并根据小时自动填充。如果你想让它是可变的(即如果你更改了label25.Text就更新(,你应该在数据表中为除数放一列,并将label25绑定到它

其他提示:

  • 字符串是列的默认类型;你不需要指定它

  • 通过在项目中添加一个DataSet对象并在其中完成所有这些,您可以让您的生活变得更轻松;它创建了强类型的数据表,这些数据表在各个方面都更容易使用


还请正确命名控件和变量-重命名控件/用合适的名称命名var需要几秒钟时间。最终,你会后悔你写代码的那一天,代码中充满了像";数据表2,列表10,标签25〃;因为它让维护变得异常困难;你将不得不不断地查找什么是什么。。

你似乎很欣赏明智地命名某些事物的概念(你把你的天和小时列称为"天"one_answers"小时",而不是"小数1"one_answers"小数2"——把这个概念扩展到你写的每一行代码(——这让其他开发人员也更容易帮助你

将其作为单独的答案发布,因为您的编辑实际上提出了一个单独的问题(如何绑定组合以便正确编辑表列,而不是如何使表列基于其他两列自动计算值(

这是一个完整的固定代码,有很好的间隔/排列和注释,这样你就可以查看它并阅读它在做什么。您可以创建一个新项目,双击它创建的空白表单,并用它替换Form1_Load代码。注意(除了Form1(,我对变量的命名比";dt2"list13"(:

private void Form1_Load(object sender, EventArgs e)
{
//set up table
DataTable timesheet = new DataTable();
timesheet.Columns.Add("Hours", typeof(decimal));
timesheet.Columns.Add("HoursInAWorkingDay", typeof(decimal));
//Add creates a column and returns it, so we can set the expression on the return value, without needing to dig it out of the column collection again
timesheet.Columns.Add("Days", typeof(decimal)).Expression = "[Hours]/[HoursInAWorkingDay]";
//set up column for Hours - dont really need this, it would autogenerate
DataGridViewTextBoxColumn hours = new DataGridViewTextBoxColumn();
hours.HeaderText = "Hours";
hours.DataPropertyName = "Hours";
//set up column for HoursInAWorkingDay
DataGridViewComboBoxColumn hoursinaworkingday = new DataGridViewComboBoxColumn();
//make array of decimal values, then use LINQ to turn them into a list of objects that have a `string Dis` and a `decimal Val` property
//these will be used during binding
var hiawd = new[] { 8m, 10m, 12m, 14m }.Select(s => new { Dis = $"{s:0}h", Val = s }).ToList();
hoursinaworkingday.HeaderText = "Work Day";
hoursinaworkingday.DataPropertyName = "HoursInAWorkingDay"; //the column in dt2 that changing the combo shall edit
hoursinaworkingday.DisplayMember = "Dis"; //show the Dis property in the combo, e.g. the string "8h"
hoursinaworkingday.ValueMember = "Val"; //use the Val property of the item selected (e.g. the decimal 8) when setting the HoursInAWorkingDay
hoursinaworkingday.DataSource = hiawd; //the list of values to use for items in the combo. tip: set datasource last
//it is important to appreciate that the combo connects the datatable to the list. it:
//1. reads the dt row's HoursInAWorkingDay, 
//2. finds the read value in the Val property of a list item, 
//3. shows that item by the Dis property. 
//4. when the user picks a new item the value of the new item's Val 
//   property is written back to the dt HoursInAWorkingDay column
//set up column for Days - dont really need this, it would autogenerate
DataGridViewTextBoxColumn days = new DataGridViewTextBoxColumn();
days.HeaderText = "Days";
days.DataPropertyName = "Days";
var dgv = new DataGridView();
dgv.Columns.Add(hours);
dgv.Columns.Add(hoursinaworkingday);
dgv.Columns.Add(days);
this.Controls.Add(dgv);
dgv.DataSource = timesheet;
dgv.Dock = DockStyle.Fill;
}

最新更新