C# Replacing value in datatable - c#

Scenario: App contains DataGridViews, I am populating the DataGridViews from a Database.
All the data in the Database is encrypted so after I fill my DataTable I need to cycle
through every entry in the DataTable through the decryption methods and place back
in the same spot in the DataTable. How would I do such a task?
Or is there a way I can decrypt the data as it is entering the dataTable?
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(query, conn);
SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(dataAdapter);
DataTable dataTable = new DataTable();
dataTable.Locale = System.Globalization.CultureInfo.Invaria…
dataAdapter.Fill(dataTable);
//Decrypt cells
int i;
foreach (DataRow row in dataTable.Rows)
{
i = 0;
foreach (var item in row.ItemArray)
{
//This doesn't work
row.ItemArray[i] = Crypto.Decrypt(item.ToString());
i++;
}
}
return dataTable;

for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
dataTable.Rows[i][j] = Crypto.Decrypt(dataTable.Rows[i][j].ToString());
}
}

Related

Incomplete XML file while attempting to save data from DataGridView into XML

I am trying to create some XML files so that in a disconnected architecture the data can be viewed directly from the XML files.
Currently, I have some data pulled in datagridview.
However, when I attempt to save this data in an XML file, the resulting file contains "" only. Nothing else, all blank.
Please suggest how can I proceed with it.
private void btnSXAAMclientinfo_Click(object sender, EventArgs e)
{
string varGUID;
varGUID = txtClientGUID.Text;
clsBusinessLogic objBL = new clsBusinessLogic();
DataSet datasetAM = objBL.LoadSXAAMClientInfo(varGUID);
dgvPatientData.DataSource = datasetAM.Tables[0].DefaultView;
DataTable dt = new DataTable();
for (int i = 1; i < dgvPatientData.Columns.Count + 1; i++)
{
DataColumn column = new DataColumn(dgvPatientData.Columns[i - 1].HeaderText);
dt.Columns.Add(column);
}
int ColumnCount = dgvPatientData.Columns.Count;
foreach (DataGridViewRow dr in dgvPatientData.Rows)
{
DataRow dataRow = dt.NewRow();
for (int i = 0; i < ColumnCount; i++)
{
dataRow[i] = dr.Cells[i];
}
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
XmlTextWriter newXml = new XmlTextWriter(#"c:/FK.xml"); ` System.Text.Encoding.UTF8);
ds.WriteXml(newXml);
}
I think you forgot to add the newly created row to the table:
foreach (DataGridViewRow dr in dgvPatientData.Rows)
{
DataRow dataRow = dt.NewRow();
for (int i = 0; i < ColumnCount; i++)
{
dataRow[i] = dr.Cells[i];
}
dt.Rows.Add(dataRow); //<--
}
Read this link for further details.

Storing filtered rows of dataGridView in a DataTable

I have a RadGridView control in my form. I bind DataGriView by one parameter by a search control. I use this code to store my selected rows in a DataTable and show in a report:
DataTable table = new DataTable("dt1");
foreach (Telerik.WinControls.UI.GridViewDataColumn column in radGridView1.Columns)
{
table.Columns.Add(column.Name, typeof(string));
}
for (int i = 0; i < radGridView1.Rows.Count; i++)
{
table.Rows.Add();
for (int j = 0; j < radGridView1.Columns.Count; j++)
{
table.Rows[i][j] = radGridView1.Rows[i].Cells[j].Value;
}
}
DataSet ds = new DataSet();
ds.Tables.Add(table);
StiReport stiReport = new StiReport();
stiReport.Load("Report.mrt");
stiReport.RegData(table);
StiOptions.Viewer.Windows.ShowPageDesignButton = false;
StiOptions.Viewer.Windows.ShowOpenButton = false;
//stiReport.Design();
stiReport.Show();
This code works, but when I use filter of RadDataGridView for selecting rows, the above code doesn't work good and all rows are displayed. How can change this code for storing only filtered rows in a DataTable.
Try making this change and you should get the desired result
DataTable table = new DataTable("dt1");
foreach (Telerik.WinControls.UI.GridViewDataColumn column in radGridView1.Columns)
{
table.Columns.Add(column.Name, typeof(string));
}
for (int i = 0; i < radGridView1.Rows.Count; i++)
{
table.Rows.Add();
for (int j = 0; j < radGridView1.Columns.Count; j++)
{
table.Rows[i][j] = radGridView1.Rows[i].Cells[j].Value;
}
}
DataSet ds = new DataSet();
ds.Tables.Add(table);
var dv = ds.Tables[0].DefaultView;
var strExpr = "ItemID = 1"; //Change accordingly
dv.RowFilter = strExpr;
var newDT = dv.ToTable();
StiReport stiReport = new StiReport();
stiReport.Load("Report.mrt");
stiReport.RegData(newDT);
StiOptions.Viewer.Windows.ShowPageDesignButton = false;
StiOptions.Viewer.Windows.ShowOpenButton = false;
//stiReport.Design();
stiReport.Show();
where strExpr should be the filtering expression that you want to use. Hope this works for you, Cheers

Don't Save csv first line as header

I'm using the following code to load my CSV file into a DataTable object. The problem is that the first line from the CSV file is loaded into the DataTable as the header row and not as a data row. How can I make all lines from the CSV file load as data rows and make datatable header row empty or any thing. This is my code
private DataTable ConvertCSVtoDataTable()
{
DataTable dataTable = new DataTable();
using (StreamReader sr = new StreamReader(csvfilename))
{
string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
{
dataTable.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',');
DataRow dr = dataTable.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dataTable.Rows.Add(dr);
}
}
return dataTable;
}
The problem is that you use the first ReadLine statement to add the Header objects. You only use the second ReadLine to populate rows in the DataTable. Try this:
private DataTable ConvertCSVtoDataTable()
{
bool firstRow = true;
DataTable dataTable = new DataTable();
using (StreamReader sr = new StreamReader(csvfilename))
{
while (!sr.EndOfStream)
{
string[] values = sr.ReadLine().Split(',');
if (firstRow)
{
firstRow = false;
for (int i = 0;i < values.Length; i++)
{
dataTable.Columns.Add("Column" + i);
}
}
DataRow dr = dataTable.NewRow();
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dataTable.Rows.Add(dr);
}
}
return dataTable;
}
EDITED:
Sorry about that I forgot to add the column headers... This should "workish". Unfortunately I dont have a C# debugger in front of me to get the syntax exact, but I believe you can figure out what I am trying to do.
You are explicitly reading the "Header". If I am understanding you correctly you simply need to remove the header management. This will leave you with columns with no header though or a null header.
private DataTable ConvertCSVtoDataTable()
{
DataTable dataTable = new DataTable();
using (StreamReader sr = new StreamReader(csvfilename))
{
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',');
DataRow dr = dataTable.NewRow();
for (int i = 0; i < rows.Length; i++)
{
dr[i] = rows[i];
}
//IF the dataTable column count is less than the row column count add some columns.
if (dataTable.Columns.size() < dr.Columns.size()){
for(int i = 0; i < dr.Columns.size(); i++){
dataTable.Columns.add("");
}
}
dataTable.Rows.Add(dr);
}
}
return dataTable;
}

SQL query to DataTable to include headers

Up until now, I had a utility class that contained a function called "getTable", which took a string query and returned a datatable. Now I'm trying to get these queries to be returned via excel. Simply copying the contents of the datatable to excel works fine - however there's no header row on the spreadsheet to explain what each column is.
Below is my attempt:
public static DataTable getTable(String sql, bool includeHeaderRow)
{
DataTable table = new DataTable();
using (SqlDataAdapter adpt = new SqlDataAdapter(sql, getConn()))
{
adpt.Fill(table);
if (includeHeaderRow)
{
DataRow headerRow = table.NewRow();
for (int i = 0; i < table.Columns.Count; i++)
{
headerRow[i] = table.Columns[i].ColumnName;
}
table.Rows.InsertAt(headerRow, 0);
}
}
return table;
}
The above almost works, but an exception is thrown when I try and write a column name (obviously a string) into a non-string column of the datatable.
Any ideas for an easy way to achieve this?
Of course the code may give error, because as you told you are assigning none compatible datatypes into one column with specific datatype, columns at Datatable accepts specific datatypes now if you try to change the column datatype to object by creating a copy of table(cloning it) I think the problem will fix:
public static DataTable getTable(String sql, bool includeHeaderRow)
{
DataTable table = new DataTable();
using (SqlDataAdapter adpt = new SqlDataAdapter(sql, getConn()))
{
adpt.Fill(table);
if (includeHeaderRow)
{
DataTable dt = table.Clone();
for (int i = 0; i < table.Columns.Count; i++)
{
dt.Columns[i].DataType = typeof(Object);
}
foreach (DataRow row in table.Rows)
{
dt.ImportRow(row);
}
DataRow headerRow = dt.NewRow();
for (int i = 0; i < table.Columns.Count; i++)
{
headerRow[i] = table.Columns[i].ColumnName;
}
dt.Rows.InsertAt(headerRow, 0);
}
}
return dt;
}

How to display rows as columns in DataGridView?

I'm trying to display set of data which have retrieved from the sql database using a datagridview in VS 2008. But I need to display data vertically rather than horizontally. This is what I have done at the beginning.
con.Open();
SqlCommand cmd = new SqlCommand("proc_SearchProfile", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#scute_id", SqlDbType.VarChar, (10)).Value = val;
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet dset = new DataSet();
adapt.Fill(dset, "Profile");
this.dataGridView1.DataSource = dset;
this.dataGridView1.DataMember = "Profile";
I searched and read a few threads but none of those work. Can anyone help me on displaying retrieved data in a datagridview vertically?
Try this:
var tbl = dset.Tables["Profile"]:
var swappedTable = new DataTable();
for (int i = 0; i <= tbl.Rows.Count; i++)
{
swappedTable.Columns.Add(Convert.ToString(i));
}
for (int col = 0; col < tbl.Columns.Count; col++)
{
var r = swappedTable.NewRow();
r[0] = tbl.Columns[col].ToString();
for (int j = 1; j <= tbl.Rows.Count; j++)
r[j] = tbl.Rows[j - 1][col];
swappedTable.Rows.Add(r);
}
dataGridView1.DataSource = swappedTable;

Categories