How to get Cell index after comparing values, for example i have this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
//If the value matches how to get the row index of that
}
}
This might do the job:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == radTextBox1.Text)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
}
If you are wanting to find the value in any cell then you need to use a nested loop.
dataGridView1.ClearSelection();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int c = 0; c < dataGridView1.Columns.Count; c++)
{
if (dataGridView1.Rows[i].Cells[c].Value.ToString() == radtextBox1.Text)
{
dataGridView1.Rows[i].Selected = true;
}
}
}
This will move through each row while checking all the columns for a value and will hilight any row where a cell has that value. Note that this code is case sensitive so "City" <> "city" but that is easily fixed using .ToUpper or .ToLower as needed.
One other thing to add, this code also is based off a DGV that has AllowUserToAddRows set to false. If you need the edit row, you either need to -1 from the count in the rows loops or check to ensure that the current row is false for .IsNewRow.
You found the Row you're looking for.
It's i variable in your code.
var requiredRowIndex = -1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
requiredRowIndex = i;
break;
}
}
if (requiredRowIndex != -1)
{
// It was found.
}
else
{
// It was not found.
}
You dont show us what is the value? It's actualy index of Cell you're looking for.
Related
I have this adjacency matrix :
And I don't know if I can check if any of the nodes are not connected with the others. I mean, if it's alone, a row and column of zeros (for example, the first one, A,) should return false because simple connectivity does not exist.
public bool HayConectividadsimple()
{
bool respuesta = true;
for (int i = 0; i < Aristas.GetLength(0); i++)
{
for (int j = 0; j < Aristas.GetLength(1); j++)
{
if (i, j == 0)
return false;
}
}
return respuesta;
}
Hope you can help me.
Best regards,
From my understanding you're looking for a whole row of 0 and a whole column of 0. If you spot either then return false. Roughly something like this:
For each node..
check if it has an all 0 column
Check if it has an all 0 row
If both are true, return false
Return true otherwise.
So, that looks like this:
public bool HayConectividadsimple()
{
// For each node..
for (int i = 0; i < Aristas.GetLength(0); i++)
{
// Assume it's not connected unless shown otherwise.
bool nodeIsConnected=false;
// Check the column and row at the same time:
for (int j = 0; j < Aristas.GetLength(1); j++)
{
if (Aristas[i, j] != 0 || Aristas[j, i] != 0)
{
// It was non-zero; must have at least one connection.
nodeIsConnected=true;
break;
}
}
// Is the current node connected?
if(!nodeIsConnected)
{
return false;
}
}
// All ok otherwise:
return true;
}
I want to get the cell number of the column "Hersteller".
I don't know how to do that and thought anyth
int columnIndex = dataGridView1.CurrentCell.ColumnIndex;
tring columnName = dataGridView1.Columns[columnIndex].Name;
for(columnName.Equals("Hersteller") columnIndex++;) { }
any ideas?
You could iterate over the columns and get the index:
int colIndex = -1;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (dataGridView1.Columns[i].Name.Equals("Hersteller"))
{
colIndex = i;
break;
}
}
if (colIndex > -1)
// Found the column, do something
I need to empty datarow from certain cell index, where previous condition has been met.
if (tabControl1.SelectedIndex == 1)
{
for (int i = 0; i < dtSecondTab.Rows.Count; i++)
{
if (dtSecondTab.Rows[i]["Month"].ToString() != "" && dtSecondTab.Rows[i]["Month"].ToString() != Convert.ToInt32(cbMonth.Text).ToString())
{
//here I need to clear datarow from cell index 6 to the end of the row
}
}
}
for(int cellIndex = 6; cellIndex < dtSecondTab.Columns.Count; cellIndex++)
{
dtSecondTab.Rows[i][cellIndex] = DBNull.Value;
}
I have DataTable and i need to clear all empty columns. So here is code example:
for (var i = 0; i < table.Columns.Count; i++)
{
if (table.Rows.Cast<DataRow>().All(r => string.IsNullOrWhiteSpace(r[i].ToStringOrNull())))
{
table.Columns.RemoveAt(i--);
}
}
Question : How can i get row Number in If-statement ?
Thanks.
If by "row number", you mean the index of a Row object in the Rows collection, then you can't get that directly from the Row object. You can, as suggested in Vlad's comment, use something like this instead:
for (var i = 0; i < table.Columns.Count; i++)
{
if (Enumerable.Range(0, table.Rows.Count).All(
j => string.IsNullOrWhiteSpace(tables.Rows[j][i].ToStringOrNull())))
{
table.Columns.RemoveAt(i--);
}
}
That said, it seems to me that it would be better to process the data such that you visit each row only once. This should at the very least improve data locality, and in some scenarios could significantly improve performance. E.g.:
bool[] keepColumn = new bool[table.Columns.Count];
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
if (!keepColumn[j] &&
!string.IsNullOrWhiteSpace(table.Columns[j].ToStringOrNull()))
{
keepColumn[j] = true;
}
}
}
for (int i = table.Columns.Count - 1; i >= 0; i--)
{
if (!keepColumn[i])
{
table.Columns.RemoveAt(i);
}
}
It's obviously a bit more verbose, but IMHO it would be worth it in this case, because it's processing the data in their natural organization (i.e. tables are naturally rows that have columns, as opposed to columns that have rows).
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";
}
}
}