I have two datagridviews:
datagridView1, with col A and B (Cells[0], Cells1):
datagridView2 (Cells[0], Cells[1], Cells[2]):
I would like to compare the value in between two tables and if:
- the value in datagridView1.rows[i].Cells[0].value = datagridView2.rows[j].Cells[0].value AND the value in datagridView1.rows[i].Cells[1].value = datagridView2.rows[j].Cells[1].value THEN write in datagridView1.rows[i].Cells[2].value the value from the third column in datagridView2:
I believe you are looking for something like this. It would depend on how you are populating your DataGridViews. There is also no error handling which should be implemented.
for (var i = 0; i < dataGridView1.Rows.Count; i++)
{
var r1 = dataGridView1.Rows[i];
var r2 = dataGridView2.Rows[i];
if (r1.Cells[0].Value == r2.Cells[0] && r1.Cells[1].Value == r2.Cells[1])
r1.Cells[2].Value = r2.Cells[2].Value;
}
Here is the second loop I've added to match any row and works fine:
for (var i = 0; i < src1.Rows.Count; i++)
{
var r1 = src1.Rows[i];
for (var j = 1; j < src2.Rows.Count; j++)
{
var r2 = src2.Rows[j];
if (r1[0].Equals(r2[0]) && r1[1].Equals(r2[1]))
{
r1[2] = r2[2];
}
}
}
Related
I am trying to fill datagridview from database (cassandra), but for some reason it shows only one row instead of two, can you tell me what I am doing wrong.
public void show_db()
{
var cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
var session = cluster.Connect("hotel");
RowSet rs = session.Execute("SELECT * FROM hotel.staff");
foreach (var res in rs)
{
for (int i = 0; 2 < 3; i++)
{
for (int j = 0; j < 4; j++)
{
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("staff_id");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("username");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("password");
staff_info.Rows[i].Cells[j].Value = res.GetValue<string>("name");
}
}
}
}
It's from database
This is from the application
I create excel file using NPOI dll's.
I have this code that create excel table from List<someObjects> :
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet 1");
IRow header = sheet1.CreateRow(0);
header.CreateCell(0).SetCellValue("Id");
header.CreateCell(1).SetCellValue("Name");
header.CreateCell(2).SetCellValue("E-Mail");
header.CreateCell(3).SetCellValue("PhoneNumber");
for (int i = 0; i < list.Count(); i++)
{
IRow row = sheet1.CreateRow(i + 1);
row.CreateCell(0).SetCellValue(list[i].id);
row.CreateCell(1).SetCellValue(list[i].name);
row.CreateCell(2).SetCellValue(list[i].email);
row.CreateCell(3).SetCellValue(list[i].phoneNumber);
}
Then I make each cell bordered in the table created above.
Here is the code:
public void setBorderExcel()
{
XSSFCellStyle myStyle = (XSSFCellStyle)workbook.CreateCellStyle();
myStyle.BorderBottom = BorderStyle.Medium;
myStyle.BorderTop = BorderStyle.Medium;
myStyle.BorderLeft = BorderStyle.Medium;
myStyle.BorderRight = BorderStyle.Medium;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle = myStyle;
}
}
}
Then I make each odd row in the table created above colored.
Here is the code:
public void setColorExcel()
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
if (i % 2 == 0) continue;
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle.FillPattern = FillPattern.SolidForeground;
}
}
}
And here is the result that I get:
As you can see the color applied to all rows in the table while, I wanted to color only the odd rows.
My question is why I get colored all rows? And how can I make colored only specific rows?
I think I understand it. You have applied the same XSSFCellStyle instance to all cells' Style property in (setBorderExcel). So, now they all have the same instance, so when you change a property on the CellStyle of one of the cells, it's changing the CellStyle instance which is associated with all cells.
You'll most likely need two XSSFCellStyle instances. One for odd rows and another for even rows.
I tried it out, and although I'm not sure why your way doesn't work, there is an easier way with less lines (simply declare another CellStyle with grey background, and use that instead of myStyle):
XSSFCellStyle myStyle = (XSSFCellStyle)workbook.CreateCellStyle();
myStyle.BorderBottom = BorderStyle.Medium;
myStyle.BorderTop = BorderStyle.Medium;
myStyle.BorderLeft = BorderStyle.Medium;
myStyle.BorderRight = BorderStyle.Medium;
XSSFCellStyle myStyleGrey = (XSSFCellStyle)workbook.CreateCellStyle();
myStyleGrey.BorderBottom = BorderStyle.Medium;
myStyleGrey.BorderTop = BorderStyle.Medium;
myStyleGrey.BorderLeft = BorderStyle.Medium;
myStyleGrey.BorderRight = BorderStyle.Medium;
myStyleGrey.FillForegroundColor = HSSFColor.Grey25Percent.Index;
myStyleGrey.FillPattern = FillPattern.SolidForeground;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
if (i % 2 == 0)
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle = myStyle;
else
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle = myStyleGrey;
}
}
You can remove your code for setColorExcel() and it should work as expected, setting odd rows to grey.
You see, I'm trying to add the quantity if the item name already exists in the DataGridView. So what I did was made a for loop to check each row in the DataGridView. What it did was just add more rows.
for (int x = 0; x < dataGridView.Rows.Count; x++)
{
if (dataGridView.Rows[x].Cells[1].Value.ToString() == dataTable.Rows[0][2].ToString())
{
dataGridView.Rows[x].Cells[0].Value = int.Parse(dataGridView.Rows[x].Cells[0].Value.ToString()) + 1;
dataGridView.Rows[x].Cells[2].Value = Convert.ToDecimal(dataTable.Rows[0][4].ToString()) * Convert.ToDecimal(dataGridView.Rows[x].Cells[0].Value.ToString());
}
else
{
quantity = 1;
dataGridView.Rows.Add(quantity, dataTable.Rows[0][2], dataTable.Rows[0][4]);
}
}
I think it's better to using .ToLower() or .ToUpper() in string comparation.
I solved it! I just made a checker using a for loop!
for (int x = 0; x < dataGridView.Rows.Count; x++)
{
if (dataGridView.Rows[x].Cells[1].Value.ToString() == dataTable.Rows[0][2].ToString())
{
same = true;
counter = x;
}
}
if (same == true)
{
dataGridView.Rows[counter].Cells[0].Value = int.Parse(dataGridView.Rows[counter].Cells[0].Value.ToString()) + 1;
dataGridView.Rows[counter].Cells[2].Value = Convert.ToDecimal(dataTable.Rows[0][4].ToString()) * Convert.ToDecimal(dataGridView.Rows[counter].Cells[0].Value.ToString());
}
else
{
quantity = 1;
dataGridView.Rows.Add(quantity, dataTable.Rows[0][2], dataTable.Rows[0][4]);
}
I have a data table that has 1 column having few cells having null value. How can I convert them to 0 ?
I bind this data table to a data grid view and cells are empty in case of null in the data table.I need to have 0 displayed in the datagridview.
Edit your cell template and set the NullValue to 0
Honestly, I'd clear this up before binding to the datagrid. I'd execute a for loop and just change all the DBNull values to 0. Its quick, easy, understandable code. This has the benefit of actually changing the data to 0, instead of changing how it is viewed to 0, as others have suggested by making tweaks to the dataGrid. Either strategy has merits, just depends on what you want to do.
Try this:
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow row = dataTable.Rows[i];
if (row["ColumnA"] == DBNull.Value)
{
row["ColumnA"] = 0;
}
}
I set the property of the DataGridViewTextBox column .Seems to be working as expected.
amountColumn.DefaultCellStyle.NullValue = "0";
If you fetch data to display from database then you might bind null to 0 using following syntax:
SELECT ISNULL(ColumnName, 0)
FROM TableName;
this works for all rows and columns with null or empty values
For i As Integer = 0 To output.Tables(0).Rows.Count - 1
Dim row As DataRow = output.Tables(0).Rows(i)
For j As Integer = 0 To output.Tables(0).Columns.Count - 1
If row(j) Is DBNull.Value Then
row(j) = 0
End If
If row(j).ToString.Length = 0 Then
row(j) = 0
End If
Next
Next
C#
for (int i = 0; i <= output.Tables[0].Rows.Count - 1; i++)
{
DataRow row = output.Tables[0].Rows[i];
for (int j = 0; j <= output.Tables[0].Columns.Count - 1; j++)
{
if (object.ReferenceEquals(row[j], DBNull.Value))
{
row[j] = 0;
}
if (row[j].ToString().Length == 0)
{
row[j] = 0;
}
}
}
Try this:
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "0";
}
}
}
I have datatable and I am displaying those values in the datagridview with the helping of code :
dataGridView1.ColumnCount = TableWithOnlyFixedColumns.Columns.Count;
dataGridView1.RowCount = TableWithOnlyFixedColumns.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
dataGridView1[j, i].Value = TableWithOnlyFixedColumns.Rows[i][j].ToString();
}
}
TableExtractedFromFile.Clear();
TableWithOnlyFixedColumns.Clear();
Now I want to save the records in the datatable in csv file.How can I do that ?
You could do this:
// we'll use these to check for rows with nulls
var columns = yourTable.Columns
.Cast<DataColumn>();
// say the column you want to sort by is called "Date"
var rows = yourTable.Select("", "Date ASC"); // or "Date DESC"
using (var writer = new StreamWriter(yourPath)) {
for (int i = 0; i < rows.Length; i++) {
DataRow row = rows[i];
// check for any null cells
if (columns.Any(column => row.IsNull(column)))
continue;
string[] textCells = row.ItemArray
.Select(cell => cell.ToString()) // may need to pick a text qualifier here
.ToArray();
// check for non-null but EMPTY cells
if (textCells.Any(text => string.IsNullOrEmpty(text)))
continue;
writer.WriteLine(string.Join(",", textCells));
}
}