且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何根据条件解析显示文本?

更新时间:2022-10-15 18:12:31

你显然需要查看数据:你需要知道当前行是什么,以及它包含什么。

没有它,你只是在猜测。

所以,在第一个 if 条件下放一个断点,然后查看CurrentRow的值和各个单元格。



我猜想有些数据不符合您的想法,或者这是在您没想到的时候执行的。



我们无法为您解决 - 我们无法访问您的数据!


根据您拥有的内容,这是正确的编码。如果执行 if 语句的第一个子句,则将跳过所有其他子句。您不应该以这种方式使用 else ,只需使用多个 if 语句,例如:

  if (dgvDisplayTiles.CurrentRow.Cells [ 4 ] .Value!=  null && 
//
{
MessageBox.Show(tilepath1);

}
if (dgvDisplayTiles.CurrentRow.Cells [ 5 ]。值!= null &&
// etc
{
MessageBox.Show(tilepath2);
}
// 依此类推


Hello,

This code generate only first tilepath message,i want to show the message according to selected image row.

if (dgvDisplayTiles.CurrentRow.Cells[4].Value != null && dgvDisplayTiles.CurrentRow.Cells[5].Value != null && dgvDisplayTiles.CurrentRow.Cells[6].Value == null && dgvDisplayTiles.CurrentRow.Cells[7].Value == null && dgvDisplayTiles.CurrentRow.Cells[8].Value == null)
{

       string str1 = dgvDisplayTiles.CurrentRow.Cells[9].Value.ToString();
    
       string[] IstImage = str1.Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
    
       //string[] lstImage = str.Split(',');
       tilepath1 = IstImage[0].ToString();
       MessageBox.Show(tilepath1);

}
else if (dgvDisplayTiles.CurrentRow.Cells[5].Value != null && dgvDisplayTiles.CurrentRow.Cells[6].Value != null && dgvDisplayTiles.CurrentRow.Cells[7].Value != null && dgvDisplayTiles.CurrentRow.Cells[8].Value != null)
{
       string str2 = dgvDisplayTiles.CurrentRow.Cells[9].Value.ToString();
    
       string[] IstImage = str2.Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
    
       tilepath1 = IstImage[0].ToString();
       tilepath2 = IstImage[1].ToString();
       MessageBox.Show(tilepath1);
       MessageBox.Show(tilepath2);
}
else if (dgvDisplayTiles.CurrentRow.Cells[6].Value != null && dgvDisplayTiles.CurrentRow.Cells[7].Value != null && dgvDisplayTiles.CurrentRow.Cells[8].Value != null)
{
       string str3 = dgvDisplayTiles.CurrentRow.Cells[9].Value.ToString();
    
       string[] IstImage = str3.Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
       tilepath1 = IstImage[0].ToString();
       tilepath2 = IstImage[1].ToString();
       tilepath3 = IstImage[2].ToString();
    
       MessageBox.Show(tilepath1);
       MessageBox.Show(tilepath2);
       MessageBox.Show(tilepath3);
}
else if (dgvDisplayTiles.CurrentRow.Cells[7].Value != null && dgvDisplayTiles.CurrentRow.Cells[8].Value != null)
{
       string str4 = dgvDisplayTiles.CurrentRow.Cells[9].Value.ToString();
    
       string[] IstImage = str4.Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
       tilepath1 = IstImage[0].ToString();
       tilepath2 = IstImage[1].ToString();
       tilepath3 = IstImage[2].ToString();
       tilepath4 = IstImage[3].ToString();
       MessageBox.Show(tilepath1);
       MessageBox.Show(tilepath2);
       MessageBox.Show(tilepath3);
       MessageBox.Show(tilepath4);

}
else if (dgvDisplayTiles.CurrentRow.Cells[8].Value != null)
{
   string str5 = dgvDisplayTiles.CurrentRow.Cells[9].Value.ToString();

       string[] IstImage = str5.Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
       tilepath1 = IstImage[0].ToString();
       tilepath2 = IstImage[1].ToString();
       tilepath3 = IstImage[2].ToString();
       tilepath4 = IstImage[3].ToString();
       tilepath5 = IstImage[4].ToString();
       MessageBox.Show(tilepath1);
       MessageBox.Show(tilepath2);
       MessageBox.Show(tilepath3);
       MessageBox.Show(tilepath4);
       MessageBox.Show(tilepath5);
}
else
{
       string str6 = dgvDisplayTiles.CurrentRow.Cells[9].Value.ToString();
    
       string[] IstImage = str6.Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
    
       tilepath1 = IstImage[0].ToString();
       tilepath2 = IstImage[1].ToString();
       tilepath3 = IstImage[2].ToString();
       tilepath4 = IstImage[3].ToString();
       tilepath5 = IstImage[4].ToString();
       tilepath6 = IstImage[5].ToString();
       MessageBox.Show(tilepath1);
       MessageBox.Show(tilepath2);
       MessageBox.Show(tilepath3);
       MessageBox.Show(tilepath4);
       MessageBox.Show(tilepath5);
       MessageBox.Show(tilepath6);
}



Thanks
Ankit Agarwal
Software Engineer

You clearly need to look at the data: you need to know what the current row is, and what it contains.
Without that, you are just guessing.
So, put a breakpoint on the first if condition, and look at the values of CurrentRow, and the individual cells.

I would guess that some data is not what you think, or this is being executed at a time you didn''t expect.

We can''t solve it for you - we don''t have access to your data!


That is correct according to what you have coded. If the first clause of the if statement is executed, then all the other clauses will be skipped. You should not be using else if in this way, just use multiple if statements, something like:
if (dgvDisplayTiles.CurrentRow.Cells[4].Value != null &&
// etc
{
       MessageBox.Show(tilepath1);
 
}
if (dgvDisplayTiles.CurrentRow.Cells[5].Value != null && 
// etc
{
       MessageBox.Show(tilepath2);
}
// and so on