I have a dataTable which has a good amount of data in it.
In certain columns, there is an apostrophe in the cell.
e.g. "Jack's Pot"
However, when I try to get this data as such:
var originalFinalShowsTable = Session["finalShowsTable"] as DataTable;
var finalShowsTable = new DataTable();
if (originalFinalShowsTable != null)
{
finalShowsTable = originalFinalShowsTable.Clone();
}
foreach (GridViewRow gvr in gvShows.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
if (((CheckBox) gvr.FindControl("cbSelect")).Checked)
{
DataRow dr = finalShowsTable.NewRow();
for (int i = 0; i < gvr.Cells.Count - 1; i++)
{
dr[i] = gvr.Cells[i + 1].Text;
}
finalShowsTable.Rows.Add(dr);
}
}
}
"Jack's Pot" turns into a "Jack's Pot".
How do I go about avoiding this?
I have seen the same occurring with spaces ( ).
HttpUtility.HtmlDecode Method
see http://msdn.microsoft.com/en-us/library/system.web.httputility.htmldecode(v=vs.100).aspx
Related
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
}
}
}
This is my code to remove row from datatable:
DataTable dtapple = dt;
foreach (DataRow drapplicant in dtapple.Rows)
{
int iapp = Convert.ToInt32(drapplicant["SrNo"].ToString());
if (drapplicant["PassportExpDate"].ToString().Trim() != "")
{
//ViewState["iapp"] = drapplicant;
dtapple.Rows.Remove(drapplicant);
}
}
Now when I use above code the row is removed, but after that I get an error
Collection was modified; enumeration operation might not execute
I don't know exact reason.
You get this error because a collection must not change while iterating it. To remove some items from a collection you usually need a second collection that you iterate without changing it.
For your DataTable you need to get the rows you want to remove first and put them in a new collection. One way to achieve this is with LINQ:
Let's create some test data:
DataTable dt = new DataTable();
dt.Columns.Add("Test", typeof(bool));
DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
DataRow dr3 = dt.NewRow();
dr1["Test"] = true;
dr2["Test"] = false;
dr3["Test"] = false;
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
then only get rows where value in the Test column is false and put them in a List<DataRow>:
var removeRows =
dt
.AsEnumerable()
.Where(r => (bool)r["Test"] == false)
.ToList();
now you can itereate the new removeRows list and remove its items from the first collection (here DataTable.Rows)
// Remove selected rows.
foreach (var row in removeRows)
{
dt.Rows.Remove(row);
}
In your this query should work:
var removeRows =
dtapple
.AsEnumerable()
.Where(r => string.IsNullOrEmpty(r["PassportExpDate"].ToString()) == false)
.ToList();
If you use string.IsNullOrEmpty() there's no need to Trim() it.
Try this :
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var tempRow = dataTable.Rows[i];
var temp = dataTable.Rows[i][0];
for (int j = 0; j < dataTable.Rows.Count; j++)
{
DataRow rows = dataTable.Rows[j];
if (temp == rows[0].ToString())
{
tempdatatable.Rows.Add(tempRow[0], tempRow[1]);
dataTable.Rows.Remove(rows); //Update happen here
}
tempdatatable.DefaultView.Sort = "gscitations DESC";
dataGridView1.DataSource = tempdatatable;
}
}
Below code works for me :
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var tempRow = dataTable.Rows[i];
var temp = dataTable.Rows[i][0];
for (int j = 0; j < dataTable.Rows.Count; j++)
{
DataRow rows = dataTable.Rows[j];
if (temp == rows[0].ToString())
{
tempdatatable.Rows.Add(tempRow[0], tempRow[1]);
dataTable.Rows.Remove(rows); //Update happen here
}
tempdatatable.DefaultView.Sort = "gscitations DESC";
dataGridView1.DataSource = tempdatatable;
}
}
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 have two Janus GridEXs. When an specific button is clicked, I want to add rows of first grid to another grid; but I want to check that already these rows do'nt exist in the second grid, so I used this code block with linq:
DataTable dtSelectedRows = new DataTable();
dtSelectedRows = firstGrid.GetDataSource().Clone();
foreach (GridEXRow row in rows)
{
DataRow dr = ((DataRowView)row.DataRow).Row;
if (dtSelectedRows.AsEnumerable().Count() > 0)
{
if (dtSelectedRows.AsEnumerable().Where(t => t.Field<Int32>("myColumn") == Convert.ToInt32(dr["myColumn"])).Count() == 0)
{
dtSelectedRows.ImportRow(dr);
}
}
else
dtSelectedRows.ImportRow(dr);
}
}
secondGrid.SetDataSource(dtSelectedRows);
but unfortunately it did'nt work and dtSelectedRows is always empty. So I forced to rewrite the block as:
GridEXRow[] rows = firstGrid.GetCheckedRows();
DataTable dtSelectedRows = new DataTable();
dtSelectedRows = firstGrid.GetDataSource().Clone();
foreach (GridEXRow row in rows)
{
if (row.RowType == Janus.Windows.GridEX.RowType.Record)
{
DataRow dr = ((DataRowView)row.DataRow).Row;
bool rowExists = false;
foreach (DataRow r in dtSelectedRows.Rows)
{
if (Convert.ToInt32(r["myColumn"]) == Convert.ToInt32(dr["myColumn"]))
{
rowExists = true;
break;
}
}
if (!rowExists)
dtSelectedRows.ImportRow(dr);}
}
secondGrid.SetDataSource(dtSelectedRows);
and fortunately it just worked. So how can I correct the first code block?
try this :
dtSelectedRows = firstGrid.GetDataSource().AsEnumerable().ToList();
instead of :
dtSelectedRows = firstGrid.GetDataSource().Clone();
I have a label, data table and a list box.
Table has columns [BUS_NAME] and [SEAT NUMBER].
And The label displays the bus name and
when the button is clicked
the values in the column [SEAT_NUMBER] whose [BUS_NAME] values are same as that of the text in the label should be listed in the list box.
i used this
for(int i = 0; i < dt.Rows.Count; i++)
{
textBox1.Text = dt.Rows[i]["Bus_NAME"].ToString();
if (lbl_busname.Text == textBox1.Text)
{
listBox1.Items.Add(dt.Rows[i]["Seat_Number"].ToString());
}
}
But this is not working. Thanks in advance..
you need to compare bus name not id. So change this line
textBox1.Text = dt.Rows[i]["Bus_ID"].ToString();
to
textBox1.Text = dt.Rows[i]["BUS_NAME"].ToString();
you can also improve this by
DataRow[] dr = dt.Select("BUS_NAME = '"+lbl_busname.Text+"'";
and loop on dr without applying any check like
foreach(DataRow d in dr)
{
listBox1.Items.Add(Convert.ToString(d["Seat_Number"]));
}
this worked
for(int i = 0; i < dt.Rows.Count; i++)
{
textBox1.Text = dt.Rows[i]["Bus_NAME"].ToString();
}
if (lbl_busname.Text == textBox1.Text)
{
listBox1.Items.Add(dt.Rows[i]["Seat_Number"].ToString());
}
else u can use the same for the following
foreach(DataRow d in dr)
{
listBox1.Items.Add(Convert.ToString(d["Seat_Number"]));
}