Access a ListView item when Clicked - c#

I am working on a Xamarin.Android proyect, I am filling a list view with a Web Service query. Now I need to access the listview's items values which I will use for future queries.
This is how I fill my listView
WebReference.ToDoWS cliente = new ToDoWS();
DataTable tabla = new DataTable();
tabla = cliente.ObtenerTareas();
ListView listado = FindViewById<ListView>(Resource.Id.tareas);
if (tabla.Rows.Count > 0)
{
List<string> tareas = new List<string>();
for (int i = 0; i < tabla.Rows.Count; i++)
{
tareas.Add(tabla.Rows[i][1].ToString());
}
ArrayAdapter<string> adaptador = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, tareas);
listado.Adapter = adaptador;
}
Here's a pick of how it looks
Is there a way to access the text values inside each row? I tried with the SetOnClickListener but I don't know how to use it at all.

I was able to access the item index with the following:
listado.ItemClick += (sender, e) =>
{
string index = e.Position.ToString();
Toast.MakeText(this, "Click" + index, ToastLength.Long).Show();
};
Documentation Referece: https://learn.microsoft.com/en-us/xamarin/android/internals/api-design#Events_and_Listeners

Related

How to pass value from datagridview to textbox

I am currently working on a system. I have a datagridview with a contextmenu and an edit and delete button on it. I want to pass the value of the selected rows to a textbox when I click the edit on contextmenu.
I have successfully passed the value to the textbox but the only values that show are from the last inputted data to whatever row I click. I don't know how to get the id, can someone please help me fix my problem? :(
Here is my code:
private void BtnEdit_Click(object sender, EventArgs e)
{
frmAddEditStudent frm = new frmAddEditStudent(this);
cn.Open();
cm = new SqlCommand("SELECT s.studentID, s.studentNo, s.Lname, s.Fname, s.MI, s.gender, s.yearLevel, s.section, s.studImage, g.name, g.contactNo, g.address FROM Student s INNER JOIN Guardian g ON g.studentNo = s.studentNo WHERE g.studentNo = s.studentNo AND s.isActive = 'true' AND s.studentID = studentID", cn);
cm.Parameters.AddWithValue("studentID", lblID.Text);
for (int i = 0; i < guna2DataGridView1.Rows.Count; i += 1)
{
frm.btnSave.Enabled = false;
frm.lblTitle.Text = "Edit Student Details";
frm.lblID.Text = guna2DataGridView1.Rows[i].Cells[1].Value.ToString();
frm.txtStudentNo.Text = guna2DataGridView1.Rows[i].Cells[2].Value.ToString();
frm.txtLname.Text = guna2DataGridView1.Rows[i].Cells[3].Value.ToString();
frm.txtFname.Text = guna2DataGridView1.Rows[i].Cells[4].Value.ToString();
frm.txtMI.Text = guna2DataGridView1.Rows[i].Cells[5].Value.ToString();
frm.cboGradeLevel.Text = guna2DataGridView1.Rows[i].Cells[7].Value.ToString();
frm.cboSection.Text = guna2DataGridView1.Rows[i].Cells[8].Value.ToString();
frm.txtGuardianName.Text = guna2DataGridView1.Rows[i].Cells[9].Value.ToString();
frm.txtContactNo.Text = guna2DataGridView1.Rows[i].Cells[10].Value.ToString();
frm.txtAddress.Text = guna2DataGridView1.Rows[i].Cells[11].Value.ToString();
//Load Image
byte[] bytes = (byte[])guna2DataGridView1.Rows[i].Cells[12].Value;
MemoryStream ms = new MemoryStream(bytes);
frm.studImage.Image = Image.FromStream(ms);
//Retrieve gender value to radio button
if (guna2DataGridView1.Rows[i].Cells[6].Value.ToString() == "Male")
{
frm.rbMale.Checked = true;
}
else
{
frm.rbFemale.Checked = true;
}
}
cn.Close();
frm.ShowDialog();
It does not show up the data in the row that I selected, instead it only shows the last row in my database table.
You can get the current row or the selected rows from a datagridview in the following way (I think ID is cell with Index 1):
Console.WriteLine(guna2DataGridView1.CurrentRow.Cells[1].Value.ToString());
foreach (DataGridViewRow loRow in guna2DataGridView1.CurrentRow.SelectedRows)
{
Console.WriteLine(loRow.Cells[1].Value.ToString());
}
But you overwrite the form values in your loop every time.
It seems that your form can only display one row and not a collection.
And what about the command cm??

Reloading live charts chart only adds new data

I'm using Live Charts in my application for a pareto chart. I have made a SeriesCollection. I'm loading it from a Stored Procedure in the following way:
public void LoadChart()
{
List<DataTops> dataTops = GetTops();
ChartValues<int> Pareto = new ChartValues<int>();
List<string> timevalues = new List<string>();
int selected = ComboSelect();
IDLables = new List<string>();
foreach (var item in dataTops)
{
values.Add(item.Total);
Pareto.Add(item.Running);
IDLables.Add((item.W) + "." + (item.B));
}
TopAlarms = new SeriesCollection
{
new ColumnSeries
{
Title = "Total",
Values =values,
DataLabels = false,
ScalesYAt = 0,
},
new LineSeries
{
Title = "%",
Values = Pareto,
ScalesYAt = 1,
PointGeometrySize = 0
}
};
public List<DataTops> GetTops()
{
int selected = ComboSelect();
DataSet Ds = new DataSet();
DataSetTableAdapters.TimePerID_TopTableAdapter TimerTopta = new DataSetTableAdapters.TimePerID_TopTableAdapter();
TimerTopta.Fill(Ds.TimePerID_Top, selected);
List<DataTops> Tops = new List<DataTops>();
foreach (DataRow row in Ds.Tables["TimePerID_Top"].Rows)
{
Tops.Add(new DataTops() { Total = (int)row["Total"], W = (int)row["W"], B = (int)row["B"], Amount = (int)row["Amount"], Running = (int)row["Running"] });
}
return Tops;
}
I have a combobox to select an amount to show (selected in the dataset) and a button that I use to update the chart. The Chart works fine, but whenever I press the update button it only adds new data behind the already existing data.
Live charts doesn't automatically clear the chart collection data upon loading so I did this:
private void UpdateChart_Click(object sender, RoutedEventArgs e)
{
if (TopAlarms != null)
{
TopAlarms.Clear();
}
LoadChart();
}
But it still won't clear and reload the chart. How can i reload the chart when the button is pressed so the new selected data amount will show?
after some testing and reasearch I have the following solution for clearing the chart:
try
{
if (TopAlarms != null)
{
TopAlarms.Clear();
}
}
catch { }
This is probably not the best solution but it works for me.

Dynamically creating a CheckBoxList

I am trying to create a dynamic menu with a title and group of checkboxes. So the output would be something like this: (pseudocode-ly)
Title 1
-checkbox1 -checkbox2 -checkbox3
Title 2
-checkbox1 -checkbox2 -checkbox3
I can get the Title back just fine, but my checkboxes are not. (See below)
Care
System.Web.UI.WebControls.CheckBoxList
Corporate & Enterprise Solutions
System.Web.UI.WebControls.CheckBoxList
I realize I am returning a DataSet, I just don't know how to handle it.
BusinessUnit bu = new BusinessUnit();
DataSet businessNames = bu.ListBusinessUnitNames();
ArrayList buNames = new ArrayList();
if (businessNames.Tables.Count > 0 && businessNames.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in businessNames.Tables[0].Rows)
{
buNames.Add(row["BSUN_NAME"].ToString());
}
}
int counter = 1;
foreach (string name in buNames)
{
Label lblName = new Label();
lblName.ID = "unitName_" + counter;
lblName.Text = name;
CheckBoxList chkBoxes = new CheckBoxList();
chkBoxes.ID = name + "Programs_" + counter;
foreach (string item in buNames)
{
DataSet buPrograms = bu.ListBusinessUnitPrograms(item);
foreach (DataRow row in buPrograms.Tables[0].Rows)
{
chkBoxes.DataTextField = row[0].ToString();
chkBoxes.Text = chkBoxes.DataTextField;
}
}
programs.InnerHtml += lblName.Text + chkBoxes;
counter++;
}
Here are the mechanics for doing it in code:
ListItem LI1 = new ListItem("aaa");
ListItem LI2 = new ListItem("bbb");
LI1.Selected = true;
LI2.Selected = false;
chkBoxes.Items.Add(LI1);
chkBoxes.Items.Add(LI2);
(Assuming you're using WebForms [aspx])
In your code example, the statement programs.InnerHtml += lblName.Text + chkBoxes; is appending the value of the default .ToString() implementation of the chkBoxes object. To actually add the checkboxes to the page, you will need some sort of container control (such as a Placeholder) on the page, and append your dynamically created control to the container's Controls collection via phPlaceholder.Controls.Add(chkBoxes)

Ways to append to GridView

I have a webapp that goes and makes some webgets and returns the results in Gridview. Sometimes, the app may need to make 400+ webgets, but only populate the grid with 15-20 records.
Is there a way to partially load a GridView, so that each record is appended to the existing GridView?
Adding Code
List<Test> testList = new List<Test>();
foreach (Location str in list)
{
string url;
try
{
url = "http://www.test.com/" + str.Url;
XmlReader reader = XmlReader.Create(url);
Rss10FeedFormatter formatter = new Rss10FeedFormatter();
if (formatter.CanRead(reader))
{
formatter.ReadFrom(reader);
IEnumerable<SyndicationItem> items = formatter.Feed.Items;
int itemCt = 1;
foreach (SyndicationItem item in items)
{
Test test = new Test();
test.Name = item.Name;
test.City= item.City;
testList.Add(data);
//if I add this here, the RowDatabound does not fire, if I take out, it works fine but only after all requests are made
listGrid.DataSource = temp;
listGrid.DataBind();
}
}
else
throw new ApplicationException("Invalid RSS 1.0 feed at " + FeedUrl.Text.Trim());
}
Create a separate list that you will DataBind the gridview to, and then whenever you change the elements in that list just rebind the gridview.
var mySmallerList = bigList.Skip(someNumber).Take(someOtherNumber);
myGridView.DataSource = mySmallerList;

Input data into a gridview with C#, ASP.net.

I am using a dataset that was created seperatley and is being used as a reference.
I need to create a gridview with data that comes from the dataset.
Coding is not the ASP.net code, but the C# code.
I just need to make one column of information.
Teacher has not taught us this and is on an assignment. If you can give me a link or type an example that would be great.
one way to doing is to bind the required columns with empty rows to the datatable and then binding the datatable to the girdview...i have given you a sample below
public void GenerateColumns()
{
dtblDummy = new DataTable("dtblDummy");
dtDummyColumn = new DataColumn("FirstName");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("LastName");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Email");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Login");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Password");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("Role");
dtblDummy.Columns.Add(dtDummyColumn);
dtDummyColumn = new DataColumn("RoleId");
dtblDummy.Columns.Add(dtDummyColumn);
}
public void GenerateRows(int intRow)
{
for(int intCounter = intRow; intCounter < intRow; intCounter++)
{
dtDummyRow = dtblDummy.NewRow();
dtDummyRow["FirstName"] = "";
dtDummyRow["LastName"] = "";
dtDummyRow["Email"] = "";
dtDummyRow["Login"] = "";
dtblDummy.Rows.Add(dtDummyRow);
}
dgrdUsers.DataSource = dtblDummy;
dgrdUsers.DataBind();
dtblDummy = null;
dtDummyRow = null;
dtDummyColumn = null;
}
in the above code dgrdUsers is the gridview control, and declare the dummy row, column and datatable above the page load function.
call the above two functions in your page load under ispostback....
dont forget the create the same no of columns as template column in your gridview...

Categories