I have this code that dynamically created my checkboxes. I have several columns of checkboxes and several rows, so it needs to be totally dynamical.
I don't know how i can read the values of the checkboxes and would like to ask for some help. BTW I would like to save the values in a multidimensional array.
Meldungtable.Columns.Add("Warnungen", typeof(string));
for (int meldungcnt = 0; meldungcnt < SPSWarnungsBausteinArray.Length; meldungcnt++)
{
Meldungtable.Rows.Add(SPSWarnungsBausteinArray[meldungcnt]);
}
WarnungenDataGridView.DataSource = Meldungtable;
for (int kameracnt = 1; kameracnt <= Kameraanzahl; kameracnt++)
{
DataGridViewCheckBoxColumn Kamerachk = new DataGridViewCheckBoxColumn();
Kamerachk.HeaderText = "Kamera" + kameracnt;
Kamerachk.Name = "KameraChkBox" + kameracnt;
Kamerachk.Width = 70;
WarnungenDataGridView.Columns.Add(Kamerachk);
}
I would read row by row and check for the checkbox name, that i have assigned before, but as I am reading the data in a different method I don't think this works out.
Please help
Display:
foreach (DataGridViewRow row in WarnungenDataGridView.Rows)
{
for (int col = 1; col < WarnungenDataGridView.ColumnCount; col++)
{
//DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)row.Cells[col];
WarnungenDataGridView.Rows[row.Index].Cells[col].Value = "true";
}
}
foreach (DataGridViewRow row in WarnungenDataGridView.Rows)
{
DataGridViewCheckBoxCell CheckedCell = row.Cells[0] as DataGridViewCheckBoxCell;
if (CheckedCell.Value != null)
{
if (CheckedCell.Value == CheckedCell.TrueValue)
{
//It's checked! do something within your code where necessary
}
}
}
Related
I would like to select my DataGridView's Rows by putting the value I'm looking for in a textbox. Also I would like the most identical value to be focused on / selected.
I already tried using the rowfilter function, which gave me this:
(dgv_DetailComptes.DataSource as DataTable).DefaultView.RowFilter = string.Format("Champ LIKE '%{0}%'", tbx_champ_Cpt.Text);
However, it filters the rows, meaning the other rows disapear when their content isn't the one I'm looking for. I would like to keep the rows in my table, and select the rows containing the value I'm looking for.
Also, my DGV takes it's Rows / Columns / values from a data Table so that might prevent me from using my DataGridView's row's index to search for the row containing the value.
Is there a way to select my rows this way ?
Thank you for your answers.
In the end I managed to find, doing maybe some useless stuff I don't know.
here's the code:
private void tbx_champ_Cpt_TextChanged(object sender, EventArgs e)
{
if (tbx_champ_Cpt.Text.ToString() == "")
{
for (int i = 0; i < dgv_DetailComptes.Rows.Count - 1; i++)
{
dgv_DetailComptes.Rows[i].Selected = false;
}
}
else
{
tbx_champ_Cpt.SelectionStart = tbx_champ_Cpt.Text.Length;
tbx_champ_Cpt.Text = tbx_champ_Cpt.Text.ToString().ToUpper();
DataTable d = (DataTable)dgv_DetailComptes.DataSource;
String text = tbx_champ_Cpt.Text.ToString();
DataRow[] row = d.Select("Champ like '%" + text + "%'");
List<int> listeIndex = new List<int>();
for (int i = 0; i < dgv_DetailComptes.Rows.Count - 1; i++)
{
foreach (DataRow r in row)
{
if (((DataRowView)dgv_DetailComptes.Rows[i].DataBoundItem).Row == r)
{
dgv_DetailComptes.Rows[i].Selected = true;
listeIndex.Add(i);
}
else if (!listeIndex.Contains(i))
{
dgv_DetailComptes.Rows[i].Selected = false;
}
}
}
}
if (dgv_DetailComptes.SelectedRows.Count != 0)
{
dgv_DetailComptes.FirstDisplayedScrollingRowIndex = dgv_DetailComptes.SelectedRows[0].Index;
}
}
datagridview dgv is bind to datatable dt.
dgv_copy is new datagridview.dgv_copy is add row to dgv by using row.clone.
my code is
if (dgv_copy.Columns.Count == 0)
{
foreach (DataGridViewColumn dgvc in dgv_org.Columns)
{
dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
}
}
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dgv_org.Rows.Count; i++)
{
row = (DataGridViewRow)dgv_org.Rows[i].Clone();
int intColIndex = 0;
foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
{
row.Cells[intColIndex].Value = cell.Value;
intColIndex++;
}
dgv_copy.Rows.Add(row);
}
dgv_copy.AllowUserToAddRows = false;
dgv_copy.Refresh();
source is https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone.aspx
Because original DataGridView is bounded to the DataTable you can create copy of DataTable and boud it to the second DataGridView.
var original = (DataTable)originalDataGridView.DataSource;
var copy = original.Copy();
copyDataGridView.DataSource = copy;
Pretty sure you could just do dgv_copy.rows.add(dgv_org.Rows[i].ItemArray)
Following issue:
I have a datatable and a list which contains specific values.
routIds = col1Items.Distinct().ToList();
String searchValue = String.Empty;
int rowIndex = -1;
for (int i = 0; i < routIds.Count; i++)
{
searchValue = routIds[i];
foreach (DataGridViewRow row in form1.dataGridView5.Rows)
{
if (row.Cells[form1.routingIdBox.Text].Value != null) // Need to check for null if new row is exposed
{
if (row.Cells[form1.routingIdBox.Text].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
foreach (DataGridViewColumn column in form1.dataGridView5.Columns)
dtRout.Columns.Add(column.Name);
for (int k = 0; k < form1.dataGridView5.Rows.Count; k++)
{
dtRout.Rows.Add();
for (int j = 0; j < form1.dataGridView5.Columns.Count; j++)
{
datTable.Rows[k][j] = form1.dataGridView5.Rows[rowIndex].Cells[j].Value;
}
}
}
}
}
}
I want to search the first column from my datagridview and check if it matches a specific value (from my array - routIds). If yes then I want to add the whole row into a datatable but I don't know how this works exactly. Tried around but I get exceptions (specific row not found).
Assuming you have a DataTable as your underlying DataSource. I would not iterate through the DataGridViewRows.
DataTable dataSource = dataGridView.DataSource as DataTable; // if it is a DataTable. If not, please specify in your question
// let's make a DataTable, which is a copy of your DataSource
DataTable dataTableFoundIds = new DataTable();
foreach (DataColumn column in dataSource.Columns)
dataTableFoundIds.Columns.Add(column.ColumnName, column.DataType);
// iterate through your routeIds
foreach (int id in routeIds)
{
var row = dataSource.AsEnumerable().FirstOrDefault(item => item["col1"].Equals(id)); // take the first row in your DataSource that matches your routeId
if (row != null)
{
dataTableFoundIds.Rows.Add(row.ItemArray); // if we find something, insert the whole row of our source table
}
}
Hope this helps!
UPDATE: if you want to find all occurances:
foreach (int id in routeIds)
{
var rows = dataSource.AsEnumerable().Where(item => item["col1"].Equals(id)); // take all rows in your DataSource that match your routeId
foreach(var row in rows)
{
dataTableFoundIds.Rows.Add(row.ItemArray); // if we find something, insert the whole row of our source table
}
}
Solution, for at least a specific cell: GridView1.Rows[i].Cells[j].Text;
I've build a simple CSV-Fileupload. After the user uploaded the file he should be able to evaluate the data. When the fileupload was successful the data gets loaded into the GridView1, with this code: (Problem below the code)
string[] readCSV = File.ReadAllLines(lblFilePath.Text);
DataTable dt = new DataTable();
bool bSplitMe = false;
foreach (var rLine in readCSV)
{
if (bSplitMe)
{
string[] aSplittedLine = rLine.Split(";".ToCharArray());
try
{
dt.Rows.Add(aSplittedLine);
}
catch(System.Exception)
{
txtBoxFileOut.Text = rLine;
break;
}
}
else
{
if (rLine.ToLower().StartsWith("definedtestid;"))
{
bSplitMe = true;
string[] aSplittedLine = rLine.Split(";".ToCharArray());
foreach (var rCol in aSplittedLine)
{
dt.Columns.Add(rCol);
}
}
else
{
txtBoxFileOut.Text += rLine.ToString() + "\n";
}
}
}
dt.Columns.Remove("Column1");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "0";
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
After this the user should be able to select a row and display the data from that row in a chart.
Problem: I'm not able to read data from the cells I want, or to read from a "hardcoded" cell.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
GridViewRow row = GridView1.SelectedRow;
txtOutputfield.Text = row.Cells[2].Text;
}
Please check your cell index. Is it correct? For example: the third column will have index "2" not "3"
And, if you use a control to store the data, you need to find that control:
txtOutputfield.Text =
row.Cells[2].FindControl('placeyourcontrolnamehere').Text;
For a specific Cell this worked fine
txtOutputfield.Text = GridView1.Rows[i].Cells[j].Text;
I am writing a web site in Visual Studio, something like an on-line library. I have a GridView on the first page that presents all of the books available from the data source and some other properties also contained in the data source. The GridView contains check boxes and the user can choose which books he wants to order by checking a box. My question is how can I use the data in the selected rows, the list of books with their properties and show that list on another page, so that the user is able to know which items he has selected?
I tried with a for loop on the FirstPage:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int bookID = (int)GridView1.DataKeys[i][0];
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (cb.Checked)
{
purchaseProductList.Add(bookID);
Response.Redirect("SecondPage.aspx?bookID" + i + "=" + bookID);
}
}
and then on the SecondPage:
for (int i = 0; i < 10; i++)
{
if (Request.QueryString["bookID" + i] != null)
{
DataRow row;
row = dtBooks.NewRow();
row["ID"] = Request.QueryString["bookID" + i];
dtBooks.Rows.Add(row);
}
}
GridView1.DataSource = dtBooks;
GridView1.DataBind();
but it didn't work. Any help? Thank you in advance.
you can use session to keep selected ids
List<string> ids = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int bookID = (int)GridView1.DataKeys[i][0];
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (cb.Checked)
{
purchaseProductList.Add(bookID);
ids.Add(bookID);
}
}
Session["Ids"] = ids;
Response.Redirect("SecondPage.aspx");
from second page you can access session and load those ids to grid
var list = (List<string>)Session["Ids"];
foreach (string id in list)
{
DataRow row;
row = dtBooks.NewRow();
row["ID"] = Request.QueryString["bookID" + id];
dtBooks.Rows.Add(row);
}
GridView1.DataSource = dtBooks;
GridView1.DataBind();