突出显示具有空字符串属性的行

时间:2019-02-27 15:40:11

标签: c# xml

假设我有一个像这样的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Logging S="T006" version="2" >
<Log Date="2018-11-21" Severity="Error" id="22" ID="Opened" Msg="some text"/>
<Log Date="2018-11-21" Severity="Info" id="76" ID="Auth"/>
<Log Date="2018-11-21" Severity="Info" id="60" ID="Up"/>
<Log Date="2018-11-21" Severity="Warning" id="22" ID="Opened" Msg="some text"/>
<Log Date="2018-11-21" Severity="Info" id="96" ID="Locked"/>
<Log Date="2018-11-21" Severity="Info" id="84" ID="Done"/>
<Log Date="2018-11-21" Severity="Error" id="16" ID="Inspected" Pos=""/>
<Log Date="2018-11-21" Severity="Error" id="10" ID="Inspected" Pos="12"/>
<Log Date="2018-11-21" Severity="Info" id="57" ID="Idle"/>
<Log Date="2018-11-21" Severity="Error" id="17" ID="Inspected" Pos=""/>
<Log Date="2018-11-21" Severity="Info" id="148" ID="Started"/>
</Logging>

我想过滤“ Pos”没有值的属性,因此它是一个空字符串,如下所示:Pos =“”
首先,我使用StreamReader解析xml文件,如下所示:

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = @"All files (*.*)|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            m_Logfile = openFileDialog1.FileName;
            StreamReader sr = new StreamReader(openFileDialog1.FileName);
            var dataset = new DataSet();
            dataset.ReadXml(sr);

            var bindingSource = new BindingSource
            {
                DataSource = dataset,
                DataMember = "Log"
            };
            dataGridView1.DataSource = bindingSource;

        }

我使用颜色对具有空Pos的行进行高亮显示,如下所示:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            string pos = dataGridView1.Rows[i].Cells["Pos"].Value.ToString();

            if(pos == "")
            {
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
            }
         }

但是结果使整个表格突出显示!不仅Pos的行为空。因此,对于此代码,我有两个问题:

  1. 如何仅突出显示具有Pos =“”属性的行?
  2. 如何添加更多条件以过滤更多Pos,例如这样的事情: if(pos == "12" && pos == "" && ID == "Opened") { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow; }我知道这行不通,但是在这种情况下,如何在一个“ if语句”中添加更多AND或&&,而不是继续添加“ else if”?

谢谢。

0 个答案:

没有答案
相关问题