check for empty gridview row C# - c#

How to check for empty gridview row. I have tried the following
for (int i = 0; i < 5; i++)
{
if(i=0 &&
(Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name").Text!="" &&
gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null)
{
// do something
}
}
I have getting an error stating that System.Web.UI.control does not contain definition for text.
How to check if the row exists and is empty or null?
Thanks

This:
if(i=0 && (Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name").
Text!="" && gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null)
Should be This:
if((i==0) && (gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null &&
(((Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name")).Text
.ToString().Trim()!=""))
Explanation :
1.for comparing values you should use == instead of single =.
2.You have to Cast the Control before reading the property values, Cast the Control as Label.
3.Trim the values before comparing strings for avoiding white space problems.
4.first do a null check before accessing the Control properties, because if control is not found it throws an Exception

Related

How to Check if an Excel Cell of Type Data Date is empty?

The Situation:
I'm running into an issue that's driving me insane. I have an Excel file, which I upload to my C# application and use Excel.Interop to run through it.
The Problem:
What I am trying to achieve is the following, if the cell I'm iterating through is not null, I treat the data inside it, else I warn the user the cell is empty/null. The issue I'm running into is the following, on of the columns in the excel is Data Type: Date, and for some reason even with an IF statement this large:
if (xlsRange.Cells[i, j] != null || xlsRange.Cells[i, j].Value != null || xlsRange.Cells[i, j].Value2 != null || xlsRange.Cells[i, j].Value != DateTime.MinValue || xlsRange.Cells[i, j].Value.ToString() != DateTime.MinValue || xlsRange.Cells[i, j].Value.ToString() != "" || xlsRange.Cells[i, j].Value.ToString() != " " || xlsRange.Cells[i, j].Value.ToString().Length != 0 || xlsRange.Cells[i, j].Value2 != DateTime.MinValue || xlsRange.Cells[i, j] != DateTime.MinValue || string.IsNullOrEmpty(xlsRange.Cells[i, j].ToString()) == false) {}
The empty cell in the Date data type columns always enter the IF Statement ... All other columns formatted with general data type work like a charm, but Date for some reason which is beyond me, does not. I'm sorry if it's a silly question but I checked a lot of posts here (therefor the huge if statement) and none of them were able to help me achieve what I need. Thanks in advance for any help that could guide me to a solution.
My expected result is that the empty Date Data Type cells do not enter the monstrous IF Statement I have built.
I my program i do check on values in each cell in entire column "I" :
try to rework for Your needs
var col2 = xlWorkSheet.UsedRange.Columns["I:I", Type.Missing];
foreach (Microsoft.Office.Interop.Excel.Range item in col2.Cells)
{
if (Convert.ToString(item.Value) == null)
{
//Change interior color for full row
item.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Cyan);
}
else
{
MessageBox.Show("Empty cell");
}
}

Finding a cell in devexpress datagrid

I'm new to DevExpress GridControl.
I need to find a particular cell in the grid and then do something with it's value.
How do I go about this please?
In the grid's Loaded method, I tried using myGrid.FindRowByValue("ProductType","ABC"), but always gives a negative number.
Thanks.
Here is code you can try
for (int i = 0; i < gridView1.DataRowCount; i++) {
object b = gridView1.GetRowCellValue(i, "FieldName");
if (b != null && b.Equals(<someValue>)){
gridView1.FocusedRowHandle = i;
return;
}
}
you can go to this link for more details.
https://www.devexpress.com/Support/Center/Question/Details/Q132599/get-row-by-cell-value
Unlike XtraGrid, the DXGrid for WPF does not provide the DataRowCount property - that is why we suggested checking the GridControl's ItemsSource. On the other hand, our grid has the VisibleRowCount property, which will be useful in some scenarios.
To accomplish this task, iterate through visible grid rows manually as shown below.
void MoveFocusToLast(GridControl grid, string fieldName, object value) {
for (int i = grid.VisibleRowCount - 1; i >= 0; i--) {
var handle = grid.GetRowHandleByVisibleIndex(i);
var currentValue = grid.GetCellValue(handle, fieldName);
if (currentValue != null && currentValue.Equals(value)) {
grid.View.FocusedRowHandle = handle;
return;
}
}
}
Grid also provides the FindRowByValue method, which allows you to
find a row by a specific cell value. This method returns the handle of
the corresponding row, and you can make that row visible by setting
the FocusedRowHandle property or calling ScrollIntoView. I
have prepared a sample demonstrating this approach.
See Also:
Traversing Rows
Find Row
Get RowHandle from a cell value

Clarification on how to use FirstDisplayedScrollingRowIndex

Just posted before and need clarification on a property. (Note I know this question is similar to others and hence I tried looking elsewhere for the solution but couldn't find the exact one for this situation).
I'm rather new to programming and don't know how to make a DataGridView scroll to the row selected by a user. I tried using FirstDisplayedScrollingRowIndex but am getting the following error:
Error: Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'int'
Which occurs when I try to bring the selected row by the user to the top of the datagridview:
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i]
Here is the full code:
String searchVal = textBox1.Text;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value != null && dataGridView1.Rows[i].Cells[0].Value.ToString().Contains(searchVal))
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i];
dataGridView1.Update();
}
}
According to the documentation, FirstDisplayedScrollingRowIndex:
Gets or sets the index of the row that is the first row displayed on the DataGridView.
You already have the index stored in i ... try just using that:
dataGridView1.FirstDisplayedScrollingRowIndex = i;
To address your second question in the comments, here's how you can find the first exact match (if any):
var selectedRow = dataGridView1.Rows.Cast<DataGridViewRow>()
.FirstOrDefault(x => Convert.ToString(x.Cells[0].Value) == searchVal);
if (selectedRow != null)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i];
dataGridView1.Update();
}
Just use i instead of dataGridView1.Rows[i]
dataGridView1.FirstDisplayedScrollingRowIndex = i;

Remove link text in Datagridview

I have a DataGridView which is populated from a list. I then Have defined a link column in the last column which is not DataBound saying View, how do I remove this link text if a condition is met. I have the below:
dgvReport.AutoGenerateColumns = false;
dgvReport.DataSource = queries;
//locals
Int32 lastColumnIndex = this.dgvReport.Columns.Count - 1;
//hide reads links for the null queries
for (int i=0; i < queries.Count; i++) {
if (queries[i].SomeID == null || queries[i].AnotherID == null) {
this.dgvReport.Rows[i].Cells[lastColumnIndex].Value = "";
}
}
This is not working and my column text still shows View. Any ideas?
Edit: lastColumnIndex is 24 which is indeed the last column index
You might try
View.PlainTextBehaviour = true;
It will remove the link and show it as plain text

Handle empty cells in DataGridView

I want to write a condition for when some cells of DataGridView are null or empty.
For example if cells[1] isn't null or empty , a method must be run and ...
I wrote it in some way , but some of them didn't work, and one of them work but its not good solution for my problem.As you now ,empty and null are different in DataGridView.
Additionally , My DataGridView has not been bound to database.
How can i do that in best way?
Regards.
DataGridViewCell object has "Value" method which returns callvalue as object, That value you can convert to string then check this string against null or empty.
string val = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string;
if(string.isNullorEmpty(val) ==false)
{
// your method run.
}
Cell value is a Object. Empty cell Value is null.
DataTable / DataView uses DbNull.Value when Empty.
Strings with Length = 0 are String.Empty
You need verify three options.
if your columns and row layout is fixed then you may call out your function in the cell validating event of the datagrid
void dataGridView2_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
e.Cancel = false;
if (e.RowIndex == 1 && e.ColumnIndex == 0)
{
if(string.IsNullorEmpty(e.FormattedValue.ToString())
// method call
}
}
//just replace from Value to FormattedValue like that
string val = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue as string;
if(string.isNullorEmpty(val))
{
// cell is empty
}
else
{
// cell is not empty
}

Categories

Resources