我在WinForms应用程序中遇到了一个问题,其中:
每当我第一次在我的组合框中选择一个项目时,随之而来的格式就会被应用。然而,在那之后,格式不再适用,我只是像下面的图片一样有空白单元格:
的数据
的
下面是我的代码:
int t = 0;
foreach (DataGridViewRow row in dataGridView2.Rows)
{
row.Cells["toleranceMinDataGridViewTextBoxColumn"].Value = "";
row.Cells["toleranceNomDataGridViewTextBoxColumn"].Value = "";
row.Cells["toleranceMaxDataGridViewTextBoxColumn"].Value = "";
row.Cells["toleranceMinDataGridViewTextBoxColumn"].ReadOnly = false;
row.Cells["toleranceNomDataGridViewTextBoxColumn"].ReadOnly = false;
row.Cells["toleranceMaxDataGridViewTextBoxColumn"].ReadOnly = false;
}
void creation()
{
dataGridView2.Columns.OfType<DataGridViewColumn>().ToList().ForEach(col => col.Visible = false);
dataGridView2.Columns["lotDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns["ItemDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns["lotDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns["ItemDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns["toleranceMinDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns["toleranceNomDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns["toleranceMaxDataGridViewTextBoxColumn"].Visible = true;
dataGridView2.Columns[$"toleranceMinDataGridViewTextBoxColumn"].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
dataGridView2.Columns[$"toleranceNomDataGridViewTextBoxColumn"].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
dataGridView2.Columns[$"toleranceMaxDataGridViewTextBoxColumn"].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
}
creation();
void creationitemsettolerances()
{
foreach (DataGridViewRow row in dataGridView2.Rows)
{
row.Visible = false;
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (!(row.Cells[$"{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value?.ToString() is ""))
{
row.Visible = true; ;
}
}
var result = from DataGridViewRow row in dataGridView2.Rows where row.Visible select row;
foreach (DataGridViewRow row in result)
{
string[] splitter = new string[3];
t++;
string a = row.Cells[$"{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value?.ToString();
if (a.Contains('-'))
{
splitter = a.Split('-');
row.Cells["toleranceMinDataGridViewTextBoxColumn"].Value = splitter[0];
row.Cells["toleranceNomDataGridViewTextBoxColumn"].Value = splitter[1];
try {
row.Cells["toleranceMaxDataGridViewTextBoxColumn"].Value = splitter[2];
}catch(Exception ex) { Debug.WriteLine(ex); }
}
else
{
row.Cells["toleranceNomDataGridViewTextBoxColumn"].Value = a;
}
}
}
creationitemsettolerances();
void formattagedestolerances()
{
var result = from DataGridViewRow row in dataGridView2.Rows where row.Visible select row;
foreach (var row in result)
{
if ((row.Cells["toleranceMinDataGridViewTextBoxColumn"].Value?.ToString() is "") && (row.Cells["toleranceMaxDataGridViewTextBoxColumn"].Value?.ToString() is ""))
{
string a = row.Cells["toleranceNomDataGridViewTextBoxColumn"].Value?.ToString();
var cell = (DataGridViewTextBoxCellEx)row.Cells["toleranceMinDataGridViewTextBoxColumn"];
cell.Value = a;
cell.ColumnSpan = 3;
cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
}
}
formattagedestolerances();
void definitiondeslots()
{
int deb1 = 0;
int deb2 = 0;
int fin1 = 0;
int fin2 = 0;
int deb3 = 0;
int fin3 = 0;
var result = from DataGridViewRow row in dataGridView2.Rows where row.Visible select row;
fin3 = result.Count() ;
foreach (var row in result)
{
if (row.Cells["TolerancesDataGridViewTextBoxColumn"].Value?.ToString() is "1")
{
if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "2") && (deb2 == 0))
{
deb2 = row.Index;
fin1 = row.Index - 1;
}
else if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "3") && (deb3 == 0))
{
fin2 = row.Index - 1;
deb3 = row.Index;
}
}
else if(row.Cells["TolerancesDataGridViewTextBoxColumn"].Value?.ToString() is "2")
{
if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "1") && (deb1 == 0))
{
deb1 = row.Index;
}
else if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "2") && (deb2 == 0))
{
deb2 = row.Index;
fin1 = row.Index - 1;
}
else if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "3") && (deb3 == 0))
{
fin2 = row.Index - 1;
deb3 = row.Index;
}
}
else if (row.Cells["TolerancesDataGridViewTextBoxColumn"].Value?.ToString() is "3")
{
if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "1") && (deb1 == 0))
{
deb1 = row.Index;
}
else if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "2") && (deb2 == 0))
{
deb2 = row.Index;
fin1 = row.Index - 1;
}
else if ((row.Cells["lotDataGridViewTextBoxColumn"].Value?.ToString() is "3") && (deb3 == 0))
{
fin2 = row.Index - 1;
deb3 = row.Index;
}
}
}
var cell1 = (DataGridViewTextBoxCellEx)dataGridView2.Rows[deb1].Cells["lotDataGridViewTextBoxColumn"];
cell1.RowSpan = fin1 - deb1+1;
var cell2 = (DataGridViewTextBoxCellEx)dataGridView2.Rows[deb2].Cells["lotDataGridViewTextBoxColumn"];
cell2.RowSpan = fin2 - deb2+1;
var cell3 = (DataGridViewTextBoxCellEx)dataGridView2.Rows[deb3].Cells["lotDataGridViewTextBoxColumn"];
cell3.RowSpan = fin3 -deb3+2;
}
definitiondeslots();
void donneesdemesures()
{
listBox1.Items.Clear();
List<int> valeurs = new List<int>();
var result = from DataGridViewRow row in dataGridView2.Rows where row.Visible select row;
foreach (var row in result)
{
string a = row.Cells[$"donnees{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value?.ToString();
if (!(a is ""))
{
valeurs.Add(Convert.ToInt32(a));
}
listBox1.Items.Add(a);
}
int b = valeurs.Max();
for (int i = 1; i <= b; i++)
{
dataGridView2.Columns.Add($"Test{i}", "Donnee");
}
foreach (var row in result)
{
for (int i = 1; i <= b; i++)
{
row.Cells[$"Test{i}"] = new DataGridViewTextBoxCellEx();
}
}
int m = 0;
foreach (var row in result)
{
for (int i = 1; i <= b; i++)
{
row.Cells[$"Test{i}"].Style.BackColor = System.Drawing.Color.Gray;
}
for (int i = 1; i <= valeurs[m]; i++)
{
row.Cells[$"Test{i}"].Style.BackColor = System.Drawing.Color.White;
}
m++;
}
}
donneesdemesures();
}
字符串
如果有人能帮上忙我会很感激的。
1条答案
按热度按时间pkmbmrz71#
感谢@Jimi的评论,我理解了单元格格式背后的整个过程。因此,为了避免任何重写,只需使用CellFormatting方法,只需遵循以下步骤:
步骤1:进入属性
x1c 0d1x的数据
第二步:点击 lightning
步骤3:查找cellformatting函数并双击它
第四步:输入密码