您好,我正在使用datagridview在c#中创建一个表单,并且我有一个类选项
public class Option
{
public string Description;
public string scores;
}
现在,我将多个选项加载到一个datagridview中,结果如下所示
|Description|Scores|
A 1-2
B 3-4
C 5-6
D 7
我想让用户能够通过点击单元格来选择分数,但由于一个描述可以有2个不同的分数,我能做的最多是做一个单独的滑块或复选框区域,让他们选择分数。我希望做的是根据该描述的分数将分数单元格拆分为不同的行。因此,description中的1行可能在score列中有2行,这是可能的吗?之后,我将使用DGV.SelectedCell根据用户选择来检索和计算分数
|Description|Score|
| | 1 |
| A |-----|
| | 2 |
|-----------------|
| | 3 |
| B |-----|
| | 4 |
|-----------------|
| | 5 |
| C |-----|
| | 6 |
|-----------------|
| D | 7 |
|-----------------|
编辑我的代码
Dictionary<int,Option> Question = new Dictionary<int,option>();
//created dictionary of options for a question
DataTable DT = new DataTable();
DT.Columns.Add("Description");
DT.Columns.Add("Scores");
foreach (KeyValuePair<int, Option> item in Question)
{ DataRow dr = DT.NewRow();
Option Opt = item.Value;
dr["Description"] = opt.Description;
dr["Scores"] = opt.scores;
}
然后,我将DGV源设置为datatable
发布于 2019-05-06 14:53:03
我在移动设备上,没有IDE,所以请原谅我,如果这不是开箱即用的编译程序,你需要稍微修改一下才能工作:
string score = dr["Weightage"].Text; //save current value from "Weightage"
string[] scores = score.split('-'); //splits it into 2 parts
DT.Columns.Remove("Weightage"); //remove old column
DT.Columns.Add("WeightageA"); //create new columns
DT.Columns.Add("WeightageB");
dr["WeightageA"] = scores[0]; //set column values
dr["WeightageB"] = scores[1];
https://stackoverflow.com/questions/55999581
复制相似问题