How to get the value of TableCell? - c#

I have a GridView that I am trying to dump into a csv file, but when I run my code the cell values are not output; my output file only consists of commas and newlines, without the cell values
I've done a bunch of google searching and I am trying to print out the values row by row, cell by cell, using a nested for loop:
String output = String.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++) {
GridViewRow currentRow = GridView1.Rows[i];
for (int j = 0; j < currentRow.Cells.Count; j++) {
output += (currentRow.Cells[j].Text + ",");
}
output += Environment.NewLine;
}
Expected output should be a comma-separated list of values with multiple rows, but the file that is actually output looks similar to this:
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
etc
Thanks in advance for any help

Related

align columns headers with cells values in a text file that exported from a datagridview in c#

I export my data from a DGV and align them with a space delimiter and it is working good for me. i also export the headers before the cells values. the thing that i want to do but i can not is aligning and adjusting the cells with headers to see them as a table that its members are separated by a delimiter (here space). my means is i want to see cells values right below the headers (if they have smaller or equal lengths) and headers text right on the top of the cells values if header length is less than cells values lengths), in the other word, Keeping cells values and their related headers together.
thanks in advance.
What i have been tried:
int RowsCount = DataTable.Rows.Count;
int ColumnsCount = DataTable.Columns.Count;
var newline = System.Environment.NewLine;
var delimiter = " ";
var copied= new StringBuilder();
List<int> clmnLocation = new List<int>() {};
string header = "";
for (int c = 0; c < DataTable.Columns.Count; c++)
{
header = header + Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ", "") + delimiter;
//Count header length to get the padright() value and put in a list<int>
clmnLocation.Add(header.Length);
}
// Export titles:
for (int c = 0; c < DataTable.Columns.Count; c++) copied.Append(Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ","") + delimiter);
//Append A Line After Header.
clipboard_string.Append(newline);
//Cells
for (int i = 0; i <RowsCount; i++)
{
for (int j = 0; j <ColumnsCount; j++)
{
if (j > 0)
copied.Append(DataTable.Rows[i][j].ToString().PadRight(clmnLocation[j]) + delimiter);
else
copied.Append(DataTable.Rows[i][j].ToString() + delimiter);
}
copied.Append(newline);
}
WordDoc.Content.Text = copied.ToString();

checking excel row values and return column header in c# winforms

I currently have program that checks a date that is selected in MonthCalendar and uses it to search through the first column [date] for a matching value before performing a function. This function is to check the rest of the columns among the same row for a certain Value and then returning the column headers that have that Value. How do I do this?
private void button1_Click(object sender, EventArgs e)
{
string date = monthCalendar1.SelectionRange.Start.ToShortDateString();
string CSVFilePathName = #"pathname.csv";
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt5 = new DataTable();
for (int i = 0; i < Cols; i++)
dt5.Columns.Add(Fields[i].ToLower(), typeof(string));
DataRow Row;
for (int i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt5.NewRow();
for (int f = 0; f < Cols; f++)
Row[f] = Fields[f];
dt5.Rows.Add(Row);
}
for (int i = 0; i < 29; i++)
{
Object o = dt5.Rows[i]["date"];
if (o.ToString() == date)
{
(INSERT CODE HERE TO CHECK REST OF COLUMNS ON SAME ROW AND RETURN COLUMN HEADER)
}
}
}
Hi I have now attached the code
You can use Microsoft Excel Interop or ClosedXML to read an excel file and then search for whatever you want.
You've said that you already have a program that does something. Can you post the code or whatever you've tried so far? It'll help us help you better.

Export Data Table To Excel

I am having a formatting issue when exporting my data table to Excel. The data is exported as it should, however if you look at my image, sometimes the cell height is increased and I am not sure why. I want the data to look the same from row to row. This is the syntax I am using to export
for (var i = 0; i < tbl.Columns.Count; i++)
workSheet.Cells[1, i + 1] = tbl.Columns[i].ColumnName;
for (var i = 0; i < tbl.Rows.Count; i++)
{
for (var j = 0; j < tbl.Columns.Count; j++)
workSheet.Cells[i + 2, j + 1] = tbl.Rows[i][j];
}
And here is an image that shows my formatting issues that I want to find a way to overcome. Can someone show me what syntax I need in order to have all row height/width the same?
Issue Image
EDIT
I tried this, but it throws an error and does not format as needed
The error is
System.Exception
Excel.Range range1 = workSheet.get_Range("A2", "S2000");
range1.EntireRow.Height.Value = 15;
You are over complicating it. Just use the UsedRange property. If workSheet is your actual variable name and 15 is the actual height you want to set, the below will work:
workSheet.UsedRange.EntireRow.RowHeight = 15;

C# Get Separate ListBox Items as strings

I'm a little bit confused on something. I wrote a code that will count the number of items in a ListBox and then write them into each cell of an excel file. Like this:
int test = ItemsList.Items.Count;
for (int i = 1; i < test; i++)
{
foreach (string itemText in ItemsList.Items)
{
worksheet.Cells[i, 0] = new Cell(itemText);
}
}
for (int i = test + 1; i < 100; i++)
{
worksheet.Cells[i, 0] = new Cell("");
}
This writes the code into excel properly however instead of displaying each item in the listbox separately it only displays the very last item in all of the cells. Any thoughts on how I can get each item from the list as a separate string for each cell?
You don't need to use 2 loops. Do something like:
for (int i = 1; i <= test; i++)
{
worksheet.Cells[i, 0] = new Cell(ItemsList.Items[i-1]);
}
You can use string.Join(string separator, IEnumerable values)
More details: http://www.dotnetperls.com/string-join http://msdn.microsoft.com/en-us/library/dd783876(v=vs.110).aspx

Simplest way of programmatically creating a DataGridView with X columns by Y rows

In C# Winforms, I'd like to use a DataGridView as a simple widget to store information to display to the user. To this end, I'd like to create a table of say, 5x10 cells.
After some research, solutions tend to allow adding just one row or column at a time. I'd like the whole grid created initially and straight away, so I can start populating it with data, like you would with a standard C# 2D array.
What's the simplest way of going about this? A function header could look like this:
createCells(DataGridView dgv, int cols, int rows) {}
It should be quick and amenable to changing the cols and rows to a larger or smaller number later on if need be.
By the way, there might an error like:
Sum Of The Columns' FillWeight Values Cannot Exceed 65535
To avoid it, set AutoGenerateColumns property to false, and set FillWeight to 1 for each column generated:
dgv.AutoGenerateColumns = false;
for (int i = 1; i <= columns; i++)
{
dgv.Columns.Add("col" + i, "column " + i);
dgv.Columns[i - 1].FillWeight = 1;
}
for (int j = 0; j < rows; j++)
dgv.Rows.Add();
You can do by using for loops in this way:
private DataGridView DGV_Creation(DataGridView dgv, int columns, int rows)
{
for (int i = 1; i <= columns; i++)
{
dgv.Columns.Add("col" + i, "column " + i);
}
for (int j = 0; j < rows; j++)
{
dgv.Rows.Add();
}
return dgv;
}
Call it with:
this.dataGridView1 = DGV_Creation(dataGridView1, 5, 10); // 5 columns, 10 rows (empty rows)
or:
private void DGV_Creation(ref DataGridView dgv, int columns, int rows)
{
for (int i = 1; i <= columns; i++)
dgv.Columns.Add("col" + i, "column " + i);
for (int j = 0; j < rows; j++)
dgv.Rows.Add();
}
call it with:
DGV_Creation(ref this.dataGridView1, 5, 10); //5 columns, 10 rows (empty rows)

Categories

Resources