i have a 10x10 text box ( 100 of them )
I write this code to write into text file :
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text)) // ignore this
{
textBox.Style["visibility"] = "hidden";
}
textBox.Enabled = false;
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
}
else
{
stringWriter.Write("," + textBox.Text );
numberofCommas++;
recordsWritten++;
}
if (recordsWritten == 10)
{
stringWriter.WriteLine();
recordsWritten = 0;
}
else
{
}
From the above i want to have 10 rows of 9 commas in the text file but instead i have 9 rows of 10 commas in the text file , is my code logic wrong? because i have been looking it for hours , i still couldn't solve it . sorry if my logic is bad , i am new to programming.
I think that you should increment recordsWritten in the last step:
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
recordsWritten++;
}
Here is a better way to do it using Linq:
var textBoxes = Panel1.Controls.OfType<TextBox>().Select((t, i) => new { TextBox = t, Index = i }).ToList();
foreach (var tb in textBoxes)
{
if (string.IsNullOrEmpty(tb.TextBox.Text))
tb.TextBox.Style["visibility"] = "hidden";
tb.TextBox.Enabled = false;
}
foreach (var line in textBoxes.GroupBy(e => e.Index / 10)
.Select(e =>
string.Join(", ",
e.Select(a => a.TextBox.Text).ToArray())))
stringWriter.WriteLine(line);
I wouldn't recommend you to use 100 TextBox objects, you can use a DataGridView binded to a DataTable with 10 rows and 10 columns. You can still edit your data and save it to a file.
Try the below code
StringWriter stringWriter1 = new StringWriter();
DataTable dataTable1 = new DataTable();
private void Form1_Shown(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
int i;
for (i = 0; i < 10; i++)
{
dataTable1.Columns.Add("Column" + (i + 1), typeof(string));
}
for (i = 0; i < 10; i++)
{
DataRow dataRow1 = dataTable1.NewRow();
dataTable1.Rows.Add(dataRow1);
}
dataGridView1.DataSource = dataTable1;
}
private void button1_Click(object sender, EventArgs e)
{
string rowString = "";
int i,j;
for (i = 0; i < 10; i++)
{
rowString = "";
for (j = 0; j < 10; j++)
{
if (dataTable1.Rows[i][j].ToString().Contains(",") == true)
{
//Enclosing the field data inside quotes so that it can
//be identified as a single entity.
rowString += "\"" + dataTable1.Rows[i][j] + "\"" + ",";
}
else
{
rowString += dataTable1.Rows[i][j] + ",";
}
}
rowString = rowString.Substring(0, rowString.Length - 1);
stringWriter1.WriteLine(rowString);
}
}
you just need to add a DataGridView onto your Form.
Related
I have a special thing to solve and can find a solution.
The Datagrid is bound to SQL query and has dynamic inner Join that is detectable all-time changing. The user can select any imported Data to show differences between up to 10 databases.
The Labelname of columns is EPreis and BPreis.
Now I need to find all EPreis and all BPreis eggs 5xBPreis 5xEPreis on Row.
Funktion1
BPreis cheapest Background color = green
Secend Preis Background Color = Yello
3...... 10.
The same on EPreis.
C#
protected void run_Click(object sender, EventArgs e)
{
SqlDataAdapter adp1 = new SqlDataAdapter(getSearch, con);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
mergeresult.DataSource = dt1;
mergeresult.DataBind();
}
protected void mergeresult_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.count - 1; i++)
{
for (int cellIndex = 0; cellIndex < Row.Cells.Count; cellIndex++)
{
if ((Label)e.row.cell.cellindex == "EPreis")
{
//? Howto find all PReis and EPreis in row.count
// is posible to put cell[x] and cell.text in array
// and at end of row sorting array 1 to x to colering
// the cell.
// Later to expand funktion Tolltip or mous over to show %
// Diverent to cheeperst
}
}
}
}
}
}
I found a solution but it still doesn't work properly.
The highest and lowest prices are sometimes reversed.
enter image description here
if (e.Row.RowIndex >= 0)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int totalRow = mergeresult.Rows.Count;
int totalColumns = mergeresult.Columns.Count;
for (int i = 0; i < totalRow + 1; i++)
{
//int anzahl = (int)ViewState["Anzahlfrom"];
//string[] NumOfPreisfields = new string[anzahl];
var Preisfields = new List<string>();
var Preisfieldslist = new List<(int cellNr, string Preis, string color)>();
for (int j = 0; j < e.Row.Cells.Count; j++)
{
var SearchColumnName = "EK";
DataControlFieldCell cell = (DataControlFieldCell)e.Row.Cells[j];
if (cell.ContainingField is BoundField)
{
if (((BoundField)cell.ContainingField).DataField.Contains(SearchColumnName))
{
Preisfields.Add(j.ToString());
Preisfieldslist.Add((j, cell.Text.ToString(), ""));
//cell.ForeColor = System.Drawing.Color.Red;
}
}
if (j == e.Row.Cells.Count -1)
{
//var SortedPreisfieldList = Preisfieldslist.OrderBy(r => r.Preis);
var LowerstPreis = Preisfieldslist.Min(r => r.Preis);
var HiegerstPreis = Preisfieldslist.Max(r => r.Preis);
for (int c = 0; c < Preisfieldslist.Count; c++)
{
if (LowerstPreis == Preisfieldslist[c].Preis)
{
int n = Preisfieldslist[c].cellNr;
e.Row.Cells[n].ForeColor = System.Drawing.Color.Green;
}
if (HiegerstPreis == Preisfieldslist[c].Preis)
{
int n = Preisfieldslist[c].cellNr;
e.Row.Cells[n].ForeColor = System.Drawing.Color.Red;
}
}
}
}
}
} // ende for (int i = 0; i < totalRow +1 ; i++)
}
As you can see from the pictures i have attached below i am having some trouble with converting this to a valid csv file. I am nearly there but cant quite figure out the last bit i need. I need to bring the column headers into the textbox and i also need to remove the ',' at the end of each line. BTW i am converting the top DGV.
here is my code -
private void btnSave_Click(object sender, EventArgs e)
{
int countRow = dgvCsv1.RowCount;
int cellCount = dgvCsv1.Rows[0].Cells.Count;
for (int rowIndex = 0; rowIndex <= countRow -1; rowIndex++)
{
for (int cellIndex = 0; cellIndex <= cellCount - 1; cellIndex++)
{
textBoxExport.Text = textBoxExport.Text + dgvCsv1.Rows[rowIndex].Cells[cellIndex].Value.ToString() + ",";
}
textBoxExport.Text = textBoxExport.Text + "\r\n";
}
System.IO.File.WriteAllText(lblFilePath.Text, textBoxExport.Text);
}
Any help greatly appreciated.
This has already been solved here. The LINQ solution does exactly what you want.
var sb = new StringBuilder();
var headers = dgvCsv1.Columns.Cast<DataGridViewColumn>();
sb.AppendLine(string.Join(",", headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));
foreach (DataGridViewRow row in dgvCsv1.Rows)
{
var cells = row.Cells.Cast<DataGridViewCell>();
sb.AppendLine(string.Join(",", cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
}
So the final code would look like:
private void btnSave_Click(object sender, EventArgs e)
{
var sb = new StringBuilder();
var headers = dgvCsv1.Columns.Cast<DataGridViewColumn>();
sb.AppendLine(string.Join(",", headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));
foreach (DataGridViewRow row in dgvCsv1.Rows)
{
var cells = row.Cells.Cast<DataGridViewCell>();
sb.AppendLine(string.Join(",", cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
}
textBoxExport.Text = sb.ToString();
System.IO.File.WriteAllText(lblFilePath.Text, textBoxExport.Text);
}
Remove last delimiter (comma)
You already have the number of columns ColumnCount, therefore, while looping through the columns, a simple check is needed to see if the currentColumn is “less than” the ColumnCount -1. If the currentColumn IS less than ColumnCount -1, then you need to add the “comma” ,. If currentColumn is NOT “less than” ColumnCount, then this can mean only one thing… this is the last column and instead of adding the comma you want to add a new line.
if (currentCol < ColumnCount - 1) {
textBoxExport.Text += ",";
}
else {
textBoxExport.Text += Environment.NewLine;
}
Column Headers in CSV file
This will need to be separate from looping through the rows. Before looping through the rows, loop through the columns of the grid and get each columns Name property. This would be the first line in the CSV file. You can use the same strategy as above to avoid the last comma.
for (int currentCol = 0; currentCol < ColumnCount; currentCol++) {
textBoxExport.Text += dgvCsv1.Columns[currentCol].Name;
if (currentCol < ColumnCount - 1) {
textBoxExport.Text += ",";
}
else {
textBoxExport.Text += Environment.NewLine;
}
}
Lastly, it is unclear why you are using a TextBox to store the CSV string. I recommend using a StringBuilder. Below is an example of what is described above.
private void btnSave_Click(object sender, EventArgs e) {
StringBuilder sb = new StringBuilder();
int RowCount = dgvCsv1.RowCount;
int ColumnCount = dgvCsv1.ColumnCount;
// get column headers
for (int currentCol = 0; currentCol < ColumnCount; currentCol++) {
sb.Append(dgvCsv1.Columns[currentCol].Name);
if (currentCol < ColumnCount - 1) {
sb.Append(",");
}
else {
sb.AppendLine();
}
}
// get the rows data
for (int currentRow = 0; currentRow < RowCount; currentRow++) {
if (!dgvCsv1.Rows[currentRow].IsNewRow) {
for (int currentCol = 0; currentCol < ColumnCount; currentCol++) {
if (dgvCsv1.Rows[currentRow].Cells[currentCol].Value != null) {
sb.Append(dgvCsv1.Rows[currentRow].Cells[currentCol].Value.ToString());
}
if (currentCol < ColumnCount - 1) {
sb.Append(",");
}
else {
sb.AppendLine();
}
}
}
}
textBoxExport.Text = sb.ToString();
System.IO.File.WriteAllText(#"D:\Test\DGV_CSV_EXPORT.csv", sb.ToString());
}
I want to store all listbox control values in a string with "," separated so that I can show it in the label. I'm using for loop but giving error
for (int i = 0; i < ListBox2.Items.Count; i++){
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0){
string projectnames += ListBox2.Items[i].ToString();
}
}
string projectnames = "";
bool firstValue = true;
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0)
{
if(!firstValue)
{
projectnames += ", " + ListBox2.Items[i].ToString();
}
else
{
projectnames += ListBox2.Items[i].ToString();
firstValue = false;
}
}
}
Instead of the loop i'd use LINQ + String.Join:
var selected = ListBox2.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.ToString());
string projectnames = String.Join(",", selected);
On that way it's much better to read and you don't need to care about trailing commas.
This will generate a string of all selected items in the list.
string projectnames = "";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
projectnames += ListBox2.Items[i].ToString() + ", ";
}
}
The most succinct method I can think of is:
var label = string.Join(",", listBox2.Items.Cast<string>());
(This uses System.Linq)
Try this, will help you!
protected void btnSave_Click(object sender, EventArgs e)
{
string selectedItem=string.Empty;
if (ListBox1.Items.Count > 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
selectedItem += ListBox1.Items[i].Text.ToString() + ", ";
//insert command
}
}
}
}
I have a datatable filled with strings.
On a column of these I want to remove the first 2 elements.
""Edit: By element i mean the first 2 characters of the string of a datacell. End of Edit""
Such thing is easy in strings, using the remove command. But is the same possible in a datacell?
PS: Some might answer "Why not define the string first, and then place it on the cell" but I'd rather avoid it, since my cells are defined by a series of concatenations. Here's how I'm doing it.
//create a list of the columns name
List<string> listadecolunas = new List<string>();
foreach (DataColumn coluna in DataAccess.Instance.tabelafinal5.Columns)
{
listadecolunas.Add(coluna.ColumnName);
}
//create a concatenated string that encolsures all the following columns
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + " + " + elemento0;
}
}
}
I didn't want to fill this with detail, but since I've posted the code, in the end I will get a datacell that begins with + and I want to remove it, it's just that.
Edit2:
My cell looks like this
I wanna remove the first + :)
You can do this with a little helper like that:
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
bool isFirst = true;
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
if(isFirst)
{
isFirst = false;
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + elemento0;
}
else
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + " + " + elemento0;
}
}
}
}
A little more efficient with a StringBuilder but same concept:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
bool isFirst = true;
sb.Clear();
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
if(isFirst)
{
isFirst = false;
}
else
{
sb.Append(" + ")
}
sb.Append(elemento0);
}
}
if(sb.Length > 0)
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + sb.ToString();
}
}
Now, if you even construct the Cell across different methods and you do not know what has happened before, you would set isFirst based on if the current value of the cell is empty or not instead of assigning true to it by default.
I have a DataGridView in a .Net application (V4 C# VS2010) & want to copy all the data to the clipboard on the click of a button. No problem -
private void copyToClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
Problem is that the user might already have some cells, rows etc selected on the DataGrid & I don't really want to change that selection. The above obviously selects everything. I could dataGridView1.ClearSelection(); at the end which is marginally better but still doesn't achieve what's required.
I can save the selected cells:
var mySelectedCells = dataGridView1.SelectedCells;
but how do I get those selected cells reselected on the DataGrid after the copy? Is there an easy way to get the selected cells collection back into the DataGrid? Perhaps there is a better way to get the whole grid copied to the clipboard in the first place without affecting presently selected cells?
I suppose if you just wanted to represent the contents of the cells as text and copy them to the clipboard, tab-delimited, you could do something like:
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i=0; i < row.Cells.Count; i++)
{
if(i == (row.Cells.Count - 1))
clipboard_string += row.Cells[i].Value + newline;
else
clipboard_string += row.Cells[i].Value + tab;
}
}
Clipboard.SetText(clipboard_string);
The output seems pretty similar to that of the GetClipboardContent(), but be careful for any DataGridViewImageColumns or any type that isn't implicitly a string.
Edit: Anthony is correct, use StringBuilder to avoid allocating a new string for every concatenation. The new code:
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = new StringBuilder();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
if (i == (row.Cells.Count - 1))
clipboard_string.Append(row.Cells[i].Value + newline);
else
clipboard_string.Append(row.Cells[i].Value + tab);
}
}
Clipboard.SetText(clipboard_string.ToString());
Here is a version of the VB code in C# with options to copy headers and to only copy selected rows.
private void CopyDataGridViewToClipboard(DataGridView dgv, bool includeHeaders = true, bool allRows = false)
{
// copies the contents of selected/all rows in a data grid view control to clipboard with optional headers
try
{
string s = "";
DataGridViewColumn oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
if (includeHeaders)
{
do
{
s = s + oCurrentCol.HeaderText + "\t";
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
while (oCurrentCol != null);
s = s.Substring(0, s.Length - 1);
s = s + Environment.NewLine; //Get rows
}
foreach (DataGridViewRow row in dgv.Rows)
{
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
if (row.Selected || allRows)
{
do
{
if (row.Cells[oCurrentCol.Index].Value != null) s = s + row.Cells[oCurrentCol.Index].Value.ToString();
s = s + "\t";
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
while (oCurrentCol != null);
s = s.Substring(0, s.Length - 1);
s = s + Environment.NewLine;
}
}
Clipboard.SetText(s);
}
catch (Exception ex)
{
toolStripStatusLabel2.Text = #"Error: " + ex.Message;
}
}
You might want to include the column headers:
private void copyAllToClipboard()
{
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = new StringBuilder();
int i;
for (i = 0; i < this.Columns.Count - 1; i++)
{
clipboard_string.Append(this.Columns[i].Name);
clipboard_string.Append(tab);
}
clipboard_string.Append(this.Columns[i].Name);
clipboard_string.Append(newline);
foreach (DataGridViewRow row in this.Rows)
{
for ( i = 0; i < row.Cells.Count - 1; i++)
{
clipboard_string.Append(row.Cells[i].Value);
clipboard_string.Append(tab);
}
clipboard_string.Append(row.Cells[i].Value);
clipboard_string.Append(newline);
}
Clipboard.SetText(clipboard_string.ToString());
}
I think below method will exactly do what you want. Just call this method with the DataGridView name at the button click event.
Private Sub CopyDataGridViewToClipboard(ByRef dgv As DataGridView)
Try
Dim s As String = ""
Dim oCurrentCol As DataGridViewColumn 'Get header
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
Do
s &= oCurrentCol.HeaderText & Chr(Keys.Tab)
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
DataGridViewElementStates.Visible, DataGridViewElementStates.None)
Loop Until oCurrentCol Is Nothing
s = s.Substring(0, s.Length - 1)
s &= Environment.NewLine 'Get rows
For Each row As DataGridViewRow In dgv.Rows
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
Do
If row.Cells(oCurrentCol.Index).Value IsNot Nothing Then
s &= row.Cells(oCurrentCol.Index).Value.ToString
End If
s &= Chr(Keys.Tab)
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
DataGridViewElementStates.Visible, DataGridViewElementStates.None)
Loop Until oCurrentCol Is Nothing
s = s.Substring(0, s.Length - 1)
s &= Environment.NewLine
Next 'Put to clipboard
Dim o As New DataObject
o.SetText(s)
Clipboard.SetDataObject(o, True)
Catch ex As Exception
ShowError(ex, Me)
End Try
End Sub
DataGridView columns can be visible/invisible and also can be displayed in different order then the order they were created. This code takes care of both:
public static void CopyGridViewToClipboard(DataGridView gvCopy)
{
if (gvCopy == null) return;
StringBuilder s = new StringBuilder();
int offset = gvCopy.ColumnHeadersVisible ? 1 : 0;
int visibleColumnsCount = 0;
//count visible columns and build mapping between each column and it's display position
Dictionary<int, int> indexMapping = new Dictionary<int, int>();
int currIndex = 0;
int lastFoundMinDisplayIndex = -1;
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
//find min DisplayIndex >= currIndex where column is visible
int minDisplayIndex = 100000;
int minDisplayIndexColumn = 100000;
for (int k = 0; k < gvCopy.ColumnCount; k++)
{
if ((gvCopy.Columns[k].Visible) && (gvCopy.Columns[k].DisplayIndex >= currIndex) && (gvCopy.Columns[k].DisplayIndex > lastFoundMinDisplayIndex))
{
if (gvCopy.Columns[k].DisplayIndex < minDisplayIndex)
{
minDisplayIndex = gvCopy.Columns[k].DisplayIndex;
minDisplayIndexColumn = k;
}
}
}
if (minDisplayIndex == 100000) break;
indexMapping.Add(minDisplayIndexColumn, currIndex);
lastFoundMinDisplayIndex = minDisplayIndex;
currIndex++;
}
visibleColumnsCount = currIndex;
//put data in temp array -- required to position columns in display order
string[,] data = new string[gvCopy.RowCount + offset, visibleColumnsCount];
if (gvCopy.ColumnHeadersVisible)
{
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
if (gvCopy.Columns[j].Visible)
{
data[0, indexMapping[j]] = gvCopy.Columns[j].HeaderText;
}
}
}
for (int i = 0; i < gvCopy.RowCount; i++)
{
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
if (gvCopy.Columns[j].Visible)
{
data[i + offset, indexMapping[j]] = gvCopy[j, i].FormattedValue.ToString();
}
}
}
//copy data
for (int i = 0; i < gvCopy.RowCount + offset; i++)
{
for (int j = 0; j < visibleColumnsCount; j++)
{
s.Append(data[i, j]);
s.Append("\t");
}
s.Append("\r\n");
}
Clipboard.SetDataObject(s.ToString());
}
You should change multiselect property of DataGridView. Here is the code:
private void copyToClipboard()
{
dataGridView1.MultiSelect = True;
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}