Checking data with commas in a DataTable - c#

I apologize for this newbie question, but I'm looking for a simple solution.
I want to write a function that will return a datatable.
Like this:
public static DataTable DataTableCommaReplce(DataTable dt){..}
The function will check each data in DataTable.
If data contained one or more commas, the function will make that data in double quote.
For Example:
you,me⇒"you,me"
What's the best way to write this function?
Can any body help me?
I had solved with this code, but I want more simple solution.
If possible, I want no looping.
public static DataTable DataTableCommaReplce(DataTable dt)
{
int col = dt.Columns.Count;
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < col; i++)
{
if (dr[i].ToString().IndexOf(",") > 0)
{
dr[i] = "\"" + dr[i].ToString() + "\"";
}
}
}
return dt;
}

This should work:
public static DataTable DataTableCommaReplce(DataTable dt) {
foreach (DataRow row in dt.Rows) {
foreach (DataColumn col in dt.Columns) {
string s = row[col] as string;
if (s != null) {
if (s.Contains(',')) {
row[col] = string.Format("\"{0}\"", s);
}
}
}
}
return dt;
}

Try the Following Code part. Hope it will help.
DataTable Tb = new DataTable();
for (int i = 0; i < Tb.Columns.Count; i++)
{
for (int j = 0; j < Tb.Rows.Count; j++)
{
if (Tb.Rows[j][i] != DBNull.Value)
{
if (Tb.Rows[j][i].ToString().IndexOf(',') != -1)
{
Tb.Rows[j][i] = "\"" + Tb.Rows[j][i].ToString() + "\"";
}
}
}
}

Related

I want to create the 'times table (2x2 = 4)' table using the GridView in Asp.net

I want to create a 9x9 GridView, list the numbers 1 through 9 in columns and rows, and then put the values ​​into an array.
I want to create it using for and Array, but I do not know what to do.
The current state is that the value is displayed below the column and all the values ​​appear only on one row.
I want to complete one program and help.
aspx.
<asp:GridView ID="GridView1" runat="server">
<Columns></Columns>
aspx.cs.
public partial class GridEX : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
int i = 1;
if (!Page.IsPostBack)
{
for (; i <= 9; i++)
{
dt.Columns.Add(i + "단");
}
for (int k = 1; k <= 9; k++)
{
DataRow dr = dt.NewRow();
for (int m = 1; m <= 9; m++)
{ dr[m] = i; }
dt.Rows.Add(dr);
dt.Rows.Add(i * k);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
What I want image:
DataTable dt = new DataTable();
dt.Columns.Add("Factor1");
dt.Columns.Add("Factor2");
dt.Columns.Add("Result");
for(int i=2; i<=9; i++)
{
for (int j=1; j<=10; j++)
{
dt.Rows.Add(i, j, i*j);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
You may have your reasons for wanting to use a Gridview here, but in my opinion, it is not the right way to achieve your goal. DataBinding is a useful tool but it does come with processing overhead, at the very least it is another iteration.
You can use a DataTable to achieve the same thing without the binding overhead.
ASPX
<asp:Table ID="MulitiplicationTable" runat="server"></asp:Table>
C#
for(int row = 0; row < 10; row++)
{
TableRow newRow = new TableRow();
MulitiplicationTable.Rows.Add(newRow);
for (int column = 0; column < 10; column++)
{
TableCell newCell = new TableCell();
newRow.Cells.Add(newCell);
//Empty cell for our first cell...
if ((row + column) == 0)
{
newCell.Text = " ";
}
//Our Column and row headers are also
//Special Cases
else if(row == 0)
{
newCell.Text = column.ToString();
newCell.CssClass = "columnHead";
}
else if(column == 0)
{
newCell.Text = row.ToString();
newCell.CssClass = "rowHead";
}
//Now do the math
else
{
newCell.Text = (row * column).ToString();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable table9x9 = new DataTable();
if (table9x9.Columns.Count == 0)
{
for (int i = 0; i < 10; i++)
{
if (i == 0)
table9x9.Columns.Add(" ");
else
table9x9.Columns.Add(i+"");
}
}
for (int row = 1; row <= 9; row++)
{
DataRow dr = table9x9.NewRow();
for (int column = 0; column <= 9; column++)
{
if (column == 0)
dr[" "] = row;
else
dr[column + ""] = row * column;
}
table9x9.Rows.Add(dr);
}
GridView2.DataSource = table9x9;
GridView2.DataBind();
}
}
Output:

How to Export DataSet to notepad?

how to Export DataSet(multiple of Datatables) to a notepad. From c#
Note: NOT A SINGLE DataTable ,Multiple DataTables From DataSet;
I would suggest something simple, create a translator or download a bunch of other libraries available on the web.
you would most like go
public interfacte IExport
{
bool Export(Databale sometable);// this can also reference interface
//concrete implementation could also handle saving of file
}
then call on a concrete class to implement that value, use a Factory Patter, Dependency Injection, etc to supply the concrete type. Then you can keep adding as many converters to support as many file types as you'd like.
private void Write(System.Data.DataSet dts, string outputFilePath)
{
System.Data.DataTable dt = new System.Data.DataTable();
for (int z = 0; z < dts.Tables.Count; z++)
{
dt = dts.Tables[z];
int[] maxLengths = new int[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
maxLengths[i] = dt.Columns[i].ColumnName.Length;
foreach (DataRow row in dt.Rows)
{
if (!row.IsNull(i))
{
int length = row[i].ToString().Length;
if (length > maxLengths[i])
{
maxLengths[i] = length;
}
}
}
}
using (StreamWriter sw = new StreamWriter(outputFilePath, true))
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2));
}
sw.WriteLine();
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (!row.IsNull(i))
{
sw.Write(row[i].ToString().PadRight(maxLengths[i] + 2));
}
else
{
sw.Write(new string(' ', maxLengths[i] + 2));
}
}
sw.WriteLine();
}
sw.Close();
}
}
}

Reading data from DataGridView in C#

How can I read data from DataGridView in C#? I want to read the data appear in Table. How do I navigate through lines?
something like
for (int rows = 0; rows < dataGrid.Rows.Count; rows++)
{
for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)
{
string value = dataGrid.Rows[rows].Cells[col].Value.ToString();
}
}
example without using index
foreach (DataGridViewRow row in dataGrid.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
string value = cell.Value.ToString();
}
}
If you wish, you can also use the column names instead of column numbers.
For example, if you want to read data from DataGridView on the 4. row and the "Name" column.
It provides me a better understanding for which variable I am dealing with.
dataGridView.Rows[4].Cells["Name"].Value.ToString();
Hope it helps.
string[,] myGridData = new string[dataGridView1.Rows.Count,3];
int i = 0;
foreach(DataRow row in dataGridView1.Rows)
{
myGridData[i][0] = row.Cells[0].Value.ToString();
myGridData[i][1] = row.Cells[1].Value.ToString();
myGridData[i][2] = row.Cells[2].Value.ToString();
i++;
}
Hope this helps....
Code Example : Reading data from DataGridView and storing it in an array
int[,] n = new int[3, 19];
for (int i = 0; i < (StartDataView.Rows.Count - 1); i++)
{
for (int j = 0; j < StartDataView.Columns.Count; j++)
{
if(this.StartDataView.Rows[i].Cells[j].Value.ToString() != string.Empty)
{
try
{
n[i, j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString());
}
catch (Exception Ee)
{ //get exception of "null"
MessageBox.Show(Ee.ToString());
}
}
}
}
private void HighLightGridRows()
{
Debugger.Launch();
for (int i = 0; i < dtgvAppSettings.Rows.Count; i++)
{
String key = dtgvAppSettings.Rows[i].Cells["Key"].Value.ToString();
if (key.ToLower().Contains("applicationpath") == true)
{
dtgvAppSettings.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}

how to looping through dataset and change data c#

im trying to loop through a dataset and where ever there is a 0 i want to change that to a null value or blank so far i have this
foreach (DataRow drRow in ds.Tables[0].Rows)
{
//loop through cells in that row
{
//if the value is 0 change to null or blank
}
}
can any one help me out of this?
Sure
foreach (DataRow drRow in ds.Tables[0].Rows)
{
for(int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
int rowValue;
if (int.TryParse(drRow[i].ToString(), out rowValue))
{
if (rowValue == 0)
{
drRow[i] = null;
}
}
}
}
I really believed that nobody still works with DataSets :(
Something like this might help:
foreach (DataRow rows in table.Rows)
{
object value = null;
var cells = rows.ItemArray;
for (int i = 0; i < cells.Length; i++)
{
value = cells[i];
if (value != null && value.GetType() == typeof(int))
{
if ((int)value == 0)
{
cells[i] = null;
}
}
}
rows.ItemArray = cells;
}
You could do
DataTable dt = new DataTable();
.....
.....
DataRowCollection drColl = dt.Rows;
for(int j=0; j<drColl.Count;j++)
{
DataRow drRow = drColl[j];
object[] itemArr = null;
itemArr = drRow.ItemArray;
//loop through cells in that row
for(int i=0; i<itemArr.Length; i++)
{
//if the value is 0 change to null or blank
if (itemArr[i].ToString() == "0")
drRow[i] = "";
}
}

Problem figuring out how if I am on the first row and column of a datatable?

.NET 2.0
Trying to find if I am on the first row so I can do a comparison
foreach(DataRow row in tbl.Rows) {
if (row<something> == "first row") { continue; }
foreach(DataColumn col in tbl.Columns) {
if (something == "first column) { continue; }
....
But it is escaping me.
Quick 'n' dirty says you can throw an int counter in there.
int rowCounter=0;
foreach(DataRow row in tbl.Rows)
{
rowCounter++;
if (rowCounter==1) { continue; }
...
//do the same for a columnCounter to get your first-column first-row
}
You can use Linq to do that:
foreach(DataRow row in tbl.Rows.Cast<DataRow>().Skip(1)) {
foreach(DataColumn col in tbl.Columns.Cast<DataColumn>().Skip(1)) {
or you can compare current Row/Column with first row or column
foreach(DataRow row in tbl.Rows) {
if (tbl.Rows[0] == "first row") { continue; }
foreach(DataColumn col in tbl.Columns) {
if (tbl.Columns[0] == "first column) { continue; }
or you can use indexer to access it (this way is faster way to access rows)
for(int rowIndex = 0; rowIndex < tbl.Rows.Count; rowIndex++)
{
if(rowIndex == 0) return;
var row = tbl.Rows[rowIndex];
}
Wont a better way be using
bool isFirstReached = false;
foreach (DataRow datarow in datatable.Rows)
{
foreach (DataColumn datacolumn in datarow.Table.Columns)
{
if (!isFirstReached)
{
isFirstReached = true;
continue;
}
}
}
unless you prefer for rather than foreach
for (int i = 0; i < datatable.Rows.Count; i++)
{
for (int j = 0; j < datatable.Columns.Count; j++)
{
if (j == 0 && i == 0)
{
continue;
}
}
}
Could you use a counter like so?
var tbl = new DataTable();
int row = -1;
int column = -1;
foreach (DataRow row in tbl.Rows)
{
row++;
if (row == 0)
{
continue;
}
foreach (DataColumn col in tbl.Columns)
{
column++;
if (column == 0)
{
continue;
}
}
}
Actually the easiest and safest way I found that someone posted on here, but removed it was:
foreach (DataRow row in tbl.Rows) {
if (tbl.Rows.IndexOf(row) < 1)
continue;
foreach (DataColumn col in tbl.Columns) {
if (tbl.Columns.IndexOf(col) < 1)
continue;

Categories

Resources