簡體   English   中英

如何在C#Windows Phone 8.1中刪除網格中的行

[英]How to delete a row in a grid in C# windows phone 8.1

我已經開始開發Windows Phone應用程序,在其中動態創建網格中的行。 在某些情況下,我需要刪除該行及其中的所有內容。

這是我的示例代碼。

        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
                }
            }

例如,如果我在第三列中獲得mytextblock.text = 0.00(在這種情況下為j = 2)。 我需要刪除第1列和第2列中的內容或刪除特定行。

我已經嘗試過“ root2.RowDefinitions.RemoveAt”,但是在那里我遇到了Arugument Exception。 我在哪里失蹤?

任何幫助將不勝感激。

最初,您的網格具有1個正在創建的行,其索引為0。但是,當您執行root2.RowDefinitions.RemoveAt((i / 6)); 實際上您正在獲取root2.RowDefinitions.RemoveAt(1); 因此,您應該使用root2.RowDefinitions.RemoveAt((i / 6)-1); 將解決異常

要回答第二個問題,代碼太混亂了。 我列出了一些

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM