2017-03-07 100 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; 

這將是在網格中的第八查看標籤的文本。