[英]How to delete a row in a grid in C# windows phone 8.1
I have started developing a windows phone application where I'm creating the row in a grid dynamically. 我已经开始开发Windows Phone应用程序,在其中动态创建网格中的行。 Upon some conditions I need to the delete the row and all its content in that row. 在某些情况下,我需要删除该行及其中的所有内容。
Here is my sample code. 这是我的示例代码。
result = e.Parameter as string;
string[] acst = result.Split('|');
int j = 0;
root2.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(10) });
for (int i = 6; i < acst.Length - 1; i++)
{
TextBlock mytextblock = new TextBlock();
mytextblock.Name = "txtDetails" + root2.Children.Count + 1;
mytextblock.Text = acst[i + 1];
if (mytextblock.Text != "")
{
if (mytextblock.Text.Trim() != "0.00") // if result is 0.00 then i have to delete all the content in that row.
{
if (j == 0)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.HorizontalAlignment = HorizontalAlignment.Stretch;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 1);
Grid.SetRow(mytextblock, (i) / 6);
j++;
}
else if (j == 1)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.Visibility = Visibility.Collapsed;
mytextblock.HorizontalAlignment = HorizontalAlignment.Center;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 2);
Grid.SetRow(mytextblock, (i) / 6);
j++;
}
else if (j == 2)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.TextWrapping = TextWrapping.Wrap;
mytextblock.HorizontalAlignment = HorizontalAlignment.Left;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 3);
Grid.SetRow(mytextblock, (i) / 6);
j=0;
root2.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(60) });
}
}
root2.Children.Add(mytextblock);
}
else
{
root2.RowDefinitions.RemoveAt((i / 6)); // here I'm getting Arugument Exception
}
}
for example if I get mytextblock.text = 0.00 in third column (j=2 in this case). 例如,如果我在第三列中获得mytextblock.text = 0.00(在这种情况下为j = 2)。 i need to remove the content in column 1 and 2 or to delete thst particular row. 我需要删除第1列和第2列中的内容或删除特定行。
I've tried "root2.RowDefinitions.RemoveAt" but there I am getting Arugument Exception. 我已经尝试过“ root2.RowDefinitions.RemoveAt”,但是在那里我遇到了Arugument Exception。 Where I'm missing? 我在哪里失踪?
Any help would be highly appreciated. 任何帮助将不胜感激。
Initially your grid has 1 row you are creating whose index is 0. But when you are executing root2.RowDefinitions.RemoveAt((i / 6));
最初,您的网格具有1个正在创建的行,其索引为0。但是,当您执行root2.RowDefinitions.RemoveAt((i / 6));
actually you are getting root2.RowDefinitions.RemoveAt(1);
实际上您正在获取root2.RowDefinitions.RemoveAt(1);
So you should use root2.RowDefinitions.RemoveAt((i / 6)-1);
因此,您应该使用root2.RowDefinitions.RemoveAt((i / 6)-1);
would solve the exception 将解决异常
To answer the second question the code is too much confusing. 要回答第二个问题,代码太混乱了。 I am listing some 我列出了一些
1: Why are you starting loop from 6 2: Why dividing i/6 3: What exactly you are getting from result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.