I am creating a Google DataTable Net Wrapper DataTable from the scratch. But I dont know how to add rows to the datatable. I have progressed to the code as below
Google.DataTable.Net.Wrapper.DataTable GDt = new Google.DataTable.Net.Wrapper.DataTable();
for (int i = 0; i < dtReports.Columns.Count; i++)
{
Google.DataTable.Net.Wrapper.Column gc = new Google.DataTable.Net.Wrapper.Column();
gc.Id = dtReports.Columns[i].Caption.Substring(0, dtReports.Columns[i].Caption.IndexOf("~"));
gc.Label = dtReports.Columns[i].Caption.Substring(dtReports.Columns[i].Caption.IndexOf("~"), dtReports.Columns[i].Caption.Length);
GDt.AddColumn(gc);
for (int j = 0; j < dtReports.Rows.Count; j++)
{
Google.DataTable.Net.Wrapper.Row gr = GDt.NewRow();
// Code to add datatable current column and row value to Google Row
}
}
Not able to find code to add the values to the row and particular column. Help appreciated.
Here is one way you could do it which fits with your existing code:
for (int j = 0; j < dtReports.Rows.Count; j++)
{
// Code to add datatable current column and row value to Google Row
Google.DataTable.Net.Wrapper.Row gr = i == 0 ? GDt.NewRow() : GDt.Rows.ElementAt(j);
gr.AddCell(new Cell(dtReports.Rows[j][i]));
if (i == 0) GDt.AddRow(gr);
}
Related
Apologies for somewhat confusing explanation. I am using Microsoft.Office.Interop.Excel. Purpose for code below is to
1) Iterate all rows in Column B of excel sheet named oSheet1
2) Iterate all rows in Column 0 of datagridview (DTG)
3) If data from Column B and Column 0 matches, then export data in Column 1 of DTG into Column C of excel.
Hence, the data in column 1 of DTG is in reference with data in Column 0 of DTG. And data in Column C of excel will eventually be in reference with Column B of excel. I've inserted some images for easy understanding
I've tried multiple codes for hours and kept getting error. Below are my codes along with errors experienced:
Error: Cannot perform in runtime binding on null reference
for (int i = 1; i <= oSheet1.Columns.Count; i++)
{
string cellvalue = oSheet1.Cells[i, 2].Value.ToString(); //error here
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((string)row.Cells[0].Value == cellvalue)
{
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
oSheet1.Cells[i, 3] = dataGridView1.Rows[j].Cells[1].Value.ToString();
}
}
}
Error: Exception from H result
for (int i = 1; i <= oSheet1.Columns.Count; i++)
{
object cellvalue = oSheet1.get_Range("B:B" + Convert.ToString(i));
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value == cellvalue)
{
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
oSheet1.Cells[i, 3] = dataGridView1.Rows[j].Cells[1].Value.ToString();
}
}
}
}
I would appreciate any help. Thank you!!
Try UsedRange.
var range = oSheet1.UsedRange;
int startingRowIndex = range.Row + 1; //to skip the header
for (int rowIndex = startingRowIndex; rowIndex < range.Row + range.Rows.Count; rowIndex++)
{
string cellvalue = range.Cells[rowIndex, 2].Value?.ToString();
...
}
Also, you should perform a null check against the Value property just in case the cell is empty, or uses a null-conditional operator as shown in the code above.
I want to transfer multiple selected rows of a DataGridView to a DataTable then set the it a datasource for my crystal report.
First I load all the data came from my database through a stored procedure.
dataGridView1.DataSource = clsPayroll.view_Employee();
Then I put the code below in the print selected button where multiselection is not restricted.
foreach(DataGridViewColumn column in dataGridView1.Columns)
table.Columns.Add(column.Name, typeof(string));
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) {
table.Rows.Add();
for (int j = 0; j < dataGridView1.Columns.Count; j++) {
table.Rows[i][j] = dataGridView1[j, i].Value;
}
}
rpt.SetDataSource(table);
I have some entries on my database here:
EMP_ID Emp_Name Gender
EMP-000013 Dummy Male
EMP-000014 Teresa Female
EMP-000015 Dutcry Male
When I select the rows EMP-000014 and EMP-0000015.
I expect that the crystalreport viewer will list it but instead it shows the EMP-000013 and EMP-000014
Thanks for those who posted their answers and I'm sorry not to update whether I've already fixed it.
I am able to figured out why my code is not working as it used to be.
Instead of fetching data from datagridview I should use datagridview.selectedrows instead.
Here's the code
foreach(DataGridViewColumn column in dataGridView1.Columns)
table.Columns.Add(column.Name, typeof(string));
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) {
table.Rows.Add();
for (int j = 0; j < dataGridView1.Columns.Count; j++) {
table.Rows[i][j] = dataGridView1.SelectedRows[i].Cells[j].Value;
}
}
rpt.SetDataSource(table);
I am trying to run a loop on a datagrid having five rows, but I am only getting the last row in the datagrid.
for (int i = 0; i < mydataGrid.Items.Count; i++)
{
var newrow = (My_DTO)mydataGrid.Items[i];
MessageBox.Show(newrow.FirstName.ToString());
}
Try using this. This should help you get the row :
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
}
Each time you call MessageBox.Show(newrow.Firstname.ToString()); you'll be overwriting the previous name.
Try:
String names = "";
for (int i = 0; i < mydataGrid.Items.Count; i++)
{
var newrow = (My_DTO)mydataGrid.Items[i];
names += newrow.FirstName.ToString());
}
MessageBox.Show(names);
That should just show the names one after the other with no spaces, but I'm not sure how you want to display the names, so that part is up to you.
I got two different Datagridviews.
Datagridview2 has just text columns. Datagridview1 contains mostly text columns and one DatagridviewComboBoxColumn. The comboxbox contains a list of all values from one column in Datagridview2.
I want to update each comboboxcell everytime Datagridview2 changes.
I’ve a read and tried a lot but i still haven’t found a way how i can change the values of a DatagridviewComboboxcell during runtime with content from another Datagridview.
To make things a bit more clear, here is a little code-snippet of how i want to change the content on a specific event (this code don’t work)
var cls = new List<string>();
if (dataGridView2.Rows.Count > 0)
{
for (int rows = 0; rows < dataGridView2.Rows.Count; rows++)
{
if (dataGridView2[0, rows].Value != null)
{
cls.Add(dataGridView2[0, rows].Value.ToString());
}
}
for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
{
DataGridViewComboBoxCell cl = new DataGridViewComboBoxCell();
var cl_content = cls;
cl.DataSource = cl_content;
var cell = dataGridView1[1, rows] as DataGridViewComboBoxCell;
cell.DataSource = cl;
}
}
I’ve tried some ideas from another post
Dynamically setting DataGridViewComboBoxCell's DataSource to filtered DataView based off of other cell selection
but nothing worked.
Maybe just copy the contents from one dataGridView to the other dataGridView when you finish editing the dataGridView.
Here is a copy dataGridViews function:
public void CopyDataGridViews(DataGridView SourceDGV, DataGridView DestinationDGV)
{
// Clear Destination DataGridView
DestinationDGV.Columns.Clear();
DestinationDGV.Rows.Clear();
// Create Destination Columns
for (int j = 0; j < SourceDGV.Columns.Count; j++)
{
DestinationDGV.Columns.Add(SourceDGV.Columns[j].Clone() as DataGridViewColumn);
}
// Create Destination Rows
for (int i = 0; i < SourceDGV.Rows.Count - 1; i++)
{
DestinationDGV.Rows.Add(SourceDGV.Rows[i].Clone() as DataGridViewRow);
}
// Copy Data to Destination
for (int column = 0; column < SourceDGV.Columns.Count; column++)
{
for (int row = 0; row < SourceDGV.Rows.Count; row++)
{
DestinationDGV.Rows[row].Cells[column].Value = SourceDGV.Rows[row].Cells[column].Value;
}
}
}
I am trying to populate my DataGridView with custom columns imported from Excel. This should be easy but for the life of me, I am stuck in a brainlock. I should be able to select the specific columns (works), send the column headers to my excel class to parse the data from that column (works), send the parsed data to a List (works) and then import the data to the datagridview by setting my DataSource as my List (I am creating the column header text, but the code is creating a column called Value and adding my data to it). Here is my code that is creating the problem:
private void btnCreateTable_Click(object sender, EventArgs e)
{
List<StringValue>[] listArr = new List<StringValue>[columnCount];
ExcelClass getData = new ExcelClass();
tabControl1.SelectedTab = tabDataTable;
dgv.Visible = true;
DataGridViewTextBoxColumn t = new DataGridViewTextBoxColumn();
//dgvSelectedHeaders.Columns[i].Name = selectedHeaders[i];
for (int i = 0; i < columnCount; i++)
{
//dgv[i].DataSource = getData.GetDataFromExcel(headersArr[i], path);
listArr[i] = getData.GetDataFromExcel(selectedHeaders[i], path);
t.Name = selectedHeaders[i];
t.HeaderText = selectedHeaders[i];
dgv.Columns.Add(t);
dgv.DataMember = "String";
dgv.DataSource = listArr[i];
for (int j = 0; j < getData.RowCount; j++)
{
dgv.Rows.Add();
for (int k = 0; k < getData.RowCount; k++)
{
dgv[k, j].Value = listArr[j][k].ToString();
}
}
}
If I select more than one column, it shows the first one but I get an error after the first one at row dgv.Columns.Add(t); ... InvalidOperationException, provided column already belongs to the DataGridView control... I know these are two questions, but I think they are tied together somehow.
Thanks in advance, and if you need more code, let me know.
I think you need to create a new DataGridViewColoumn for each column
for (int i = 0; i < columnCount; i++)
{
DataGridViewTextBoxColumn t = new DataGridViewTextBoxColumn();
// ...
dgv.Columns.Add(t);
}
rather than reusing it.