Hello,
I have created a form with grid which contains button and label.So the number or rows and columns vary according to my table.
i haved binded label and button text. now when i click a button i want the data which is binded to that clicked button.
Since number of rows and columns vary i have done this in code behind.Please help
<List> table=new <List>();
protected async override void OnAppearing()
{
//data for table is getting using webapi. im not giving that code
count= table.Count;
decimal rw = (decimal)table.Count / 3;
decimal RowCountRequired = Decimal.Ceiling(rw);
var maxwidth = Application.Current.MainPage.Width;
var width = maxwidth / 3;
for (int currentRow = 0; currentRow < RowCountRequired; currentRow++)
{
count = count - ColumnCountRequired;
for (int currentColumn = 0; currentColumn < ColumnCountRequired; currentColumn++)
{
var label = new Label
{
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center
};
label.SetBinding(Label.TextProperty, nameof(Table_Master.th_tableid));
gridLayout.Children.Add(label, currentColumn, currentRow);
}
}
ColumnCountRequired = 3;
count = table.Count;
for (int currentRow = 0; currentRow < RowCountRequired; currentRow++)
{
gridLayout.RowDefinitions.Add(new RowDefinition { Height = 250});
gridLayout.VerticalOptions = LayoutOptions.FillAndExpand;
gridLayout.HorizontalOptions = LayoutOptions.FillAndExpand;
if (count < 3)
{
ColumnCountRequired = count;
}
count = count - ColumnCountRequired;
for (int currentColumn = 0; currentColumn < ColumnCountRequired; currentColumn++)
{
gridLayout.ColumnDefinitions.Add(new ColumnDefinition { Width =width});
Button button = new Button
{
TextColor=Color.White,
BackgroundColor = Color.Green,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
button.SetBinding(Label.TextProperty, nameof(Table_Master.th_tablename));
button.Clicked += OnButtonClicked;
gridLayout.Children.Add(button , currentColumn, currentRow);
}
// reduce the number of columns on the next row to simulate row span
// ColumnCountRequired = ColumnCountRequired - 1;
}
for (int i=0;i<table.Count; i++)
{
gridLayout.Children[i].BindingContext = table[i];
}
base.OnAppearing();
}
private void OnButtonClicked(object sender, EventArgs e)
{
// if i click row0,col0 th button i want data binded to label o ro0,col0
}