本文介绍了如何根据条件解析显示文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 此代码仅生成第一个tilepath消息,我想根据所选图像行显示消息。 if (dgvDisplayTiles.CurrentRow.Cells [ 4 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 5 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 6 ]。值== null && dgvDisplayTiles.CurrentRow.Cells [ 7 ]。值== null & ;& dgvDisplayTiles.CurrentRow.Cells [ 8 ]。值== null ) { string str1 = dgvDisplayTiles.CurrentRow.Cells [ 9 ]。值。的ToString(); string [] IstImage = str1.Replace( ,, )。替换( \\Library \\, ,)。删除( 0 , 1 ).Split(' ,'); // string [] lstImage = str.Split(','); tilepath1 = IstImage [ 0 ]。ToString(); MessageBox.Show(tilepath1); } else if (dgvDisplayTiles.CurrentRow.Cells [ 5 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 6 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 7 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 8 ] .Value!= null ) { string str2 = dgvDisplayTiles.CurrentRow .Cells [ 9 ]。Value.ToString(); string [] IstImage = str2.Replace( ,, )。替换( \\Library \\, ,)。删除( 0 , 1 ).Split(' ,'); tilepath1 = IstImage [ 0 ]。ToString(); tilepath2 = IstImage [ 1 ]。ToString(); MessageBox.Show(tilepath1); MessageBox.Show(tilepath2); } else if (dgvDisplayTiles.CurrentRow.Cells [ 6 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 7 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 8 ]。值!= null ) { string str3 = dgvDisplayTiles。 CurrentRow.Cells [ 9 ]。Value.ToString(); string [] IstImage = str3.Replace( ,, )。替换( \\Library \\, ,)。删除( 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 ]。值!= null && dgvDisplayTiles.CurrentRow.Cells [ 8 ]。值!= null ) { string str4 = dgvDisplayTiles.CurrentRow.Cells [ 9 ]。Value.ToString(); string [] IstImage = str4.Replace( ,, )。替换( \\Library \\, ,)。删除( 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 ]。值!= null ) { string str5 = dgvDisplayTiles.CurrentRow.Cells [ 9 ]。Value.ToString(); string [] IstImage = str5.Replace( ,, )。替换( \\Library \\, ,)。删除( 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( ,, )。替换( \\Library \\, ,)。删除( 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); } 谢谢 Ankit Agarwal 软件工程师解决方案 你显然需要查看数据:你需要知道当前行是什么,以及它包含什么。 没有它,你只是在猜测。 所以,在第一个 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);}ThanksAnkit AgarwalSoftware 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 这篇关于如何根据条件解析显示文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 16:02
查看更多