2017-03-07 40 views
0

我已经通过编程生成了包含标签和文本框的网格。代码显示如下xamarin格式 - 在网格中获取文本框的值

grid.Children.Add(new Label { Text = "ID", BackgroundColor = Color.Black, IsVisible = false, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 0, 1, 0, 1); 
grid.Children.Add(new Label { Text = "Desc", BackgroundColor = Color.Black, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 1, 2, 0, 1); 
grid.Children.Add(new Label { Text = "Qty", BackgroundColor = Color.Black, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 2, 3, 0, 1); 
grid.Children.Add(new Label { Text = "Receive Qty", BackgroundColor = Color.Black, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 4, 6, 0, 1); 

for (int i = 0; i < 10; i++) 
{ 
    sID = e.Result[i].ID.ToString(); 
    sQty = e.Result[i].Qty.ToString(); 
    sDesc = e.Result[i].DESC1.ToString(); 
    if(e.Result[i].ReceivedQty.ToString() == "0") 
    { 
     sReceivedQty = sQty; 
    } 
    else 
    { 
     sReceivedQty = e.Result[i].ReceivedQty.ToString(); 
    } 

    grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }); 
    grid.Children.Add(new Label { Text = sID, IsVisible = false, BackgroundColor = Color.Gray, TextColor = Color.White, FontSize = 
     Device.GetNamedSize(NamedSize.Micro, typeof(Label)) }, 0, 1, i + 1, i + 2); 
    grid.Children.Add(new Label { Text = sDesc, BackgroundColor = Color.Gray, TextColor = Color.White, FontSize = 
     Device.GetNamedSize(NamedSize.Micro, typeof(Label)) }, 1 2, i + 1, i + 2); 
    grid.Children.Add(new Label { Text = sQty, BackgroundColor = Color.Gray, TextColor = Color.White, FontSize = 
     Device.GetNamedSize(NamedSize.Micro, typeof(Label)) }, 2, 3, i + 1, i + 2); 
    grid.Children.Add(new Entry { Text = sReceivedQty, TextColor = Color.White, FontSize = 10 }, 3, 4, i + 1, i + 2); 
} 

我希望在每个行的文本框中获取值?请帮忙!

回答

1

您无法查询Grid以获取第X行的元素,但您可以查询Grid中的元素并询问它们的位置。

//get the element at position r, c 
var view = grid.Children.FirstOrDefault(v => Grid.GetRow(v) == r && Grid.GetColumn(v) == c); 
var label = view as Label; 
if (label != null) { 
    //label.Text is the string you're looking for 
} 
+0

如何知道位置r和c,因为我不知道行索引。我面临另一个问题。我希望将用户在文本框中输入的数据更新回数据库。我不知道如何才能知道文本框中的数据属于哪个ID。你有什么想法如何确认身份证属于同一行的文本框? –

0
int index=8; 
string y = ((Label)gridName.Children.ElementAt(index)).Text; 

这将是在网格中的第八查看标签的文本。