Hide a cell from gridview - c#

I have a winform c# SQL app in which i retrieve some values to a datagrid view and from there on wards i will display them to a user.
however there are certain gridcells which have values that i don't want to show to the user, and i want to hide them,
here is my code to hide cell values.
CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource=dmz.Tables[0]];
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells[51].Value.ToString() == "N/A") //object reference not set to an instance of the object exception is thrown
{
cm.SuspendBinding();
dgvr.Visible = false;
}
there is an object reference not set to an instance of the object exception thrown...
what is triggering the error?
Please help...

Change
if (dgvr.Cells[51].Value.ToString() == "N/A")
to
if (dgvr.Cells[51].Value??"").ToString() == "N/A")
or
if (dgvr.Cells[51].Value!=null && dgvr.Cells[51].Value.ToString() == "N/A")

You can use the DataGridView_CellFormatting event method .
Like this:
private void dgvr_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(dgvr.Rows[e.RowIndex].Cells["51"].Value!=null && dgvr[51].Value.ToString()=="N/A")
{
dgvr.Rows[e.RowIndex].Cells["51"].Value="";
}
}

Related

How to get current row of datagridview values when clicking on button?

I have a View Model in which my data is stored to be used on the presenter and window. However, I now need to get (when pressing a button) the current row's results on the grid into my view model. I first used the mouse double click event, but the requirements changed from that to a button on the form:
private void dgvResults_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (this.colOrderNo.Index != e.ColumnIndex || e.RowIndex < 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) this.dgvResults.Rows[e.RowIndex].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
}
That's my old code. Now I need that here:
private void btnAuditLog_Click(object sender, EventArgs e)
Not sure how to refer to the RowIndex without the DataGridViewCellMouseEventArges event. I tried the CurrentRow property, but it doesn't do the job.
Just another question on the fly, if someone could assist with that as well, how do I get a value from one form to another, spesifically a properties value. It's not on the grid, in a control. It's basically just public string order that's all. I set the value on one form, then need it for query filtering on another. Let me know if you need anything else.
You want to use the SelectedRows property of the DataGridView. It tells which row is selected. In this case, you seem to want the first selected row (since you have a single property called this.AuditOrderKey).
The column order no to longer matters in this context.
if (dgvResults.SelectedRows.Count == 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.SelectedRows[0].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
If you don't have selected rows, CurrentRow is an option:
if (dgvResults.CurrentRow == null) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.CurrentRow.DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
Use the SelectedRows property of the DataGridView. Assuming that you set your DataGridView as MultiSelect = false; and SelectionMode = FullRowSelect;
if (dgvResults.SelectedRows.Count > 0)
{
dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
In your button click event, it will look like this. (Assuming that your button name is btnEdit)
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvResults.SelectedRows.Count > 0)
{
string selectedValue = dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
}

how to set Check Repository radio group in gridcontrol devexpress using C# from value another table

i have repository radio group in grid control cell, i wanna set checked in repository radio while value from another cell. this my sample:
while ISJAWAB value is 1, repository radio group will be checked in column PILIH.
i have tried simple code but it doesn't work:
private void gridView2_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.FieldName == "PILIH")
{
var val = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "ISJAWAB"));
if (val == "1")
{
e.DisplayText = "1"; //nilai 1 untuk select radio
}
}
}
in the repository collection i have set value to 1, and it automatically set check in repository radio group.
need help ??
RadioGroup is not the best choice for the in-place editor in this case, as it gives you redundant functionality to display several choices within single cell. For your purpose you can use CheckEdit. Also, with CustomDrawCell event you will have to actually manually redraw your radio buttons. There's an easier solution utilizing unbound columns.
So I suggest the following solution:
1.Add CheckEdit repository item, set it's CheckStyle property equal to Radio.
2.Make your PILIH column's Unbound Type = Boolean. Assign above mentioned CheckEdit repository item to it's ColumnEdit. Set OptionsColumn.AllowEdit = false.
3.Add gridView2.CustomUnboundColumnData event handler with body like following:
private void gridView2_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.IsGetData && e.Column.FieldName == "PILIH")
{
var row = (DataRow) e.Row;
var val = Convert.ToString(row["ISJAWAB"]));
if (val == "1")
{
e.Value = true;
}
}
}
you must change the type of reposityRadio to Boolean to true instead of 1 :
2nd the PILIH column data source must be type bool
and use this code :
private void gridView2_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.FieldName == "PILIH")
{
var val = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "ISJAWAB"));
if (val == "1")
{
gridView2.SetRowCellValue(e.RowHandle, "PILIH", true);
}
}
}
the result should be like this :

How to hide Rows In GridView

I have came across the we and check different web pages but I didnt find the one I was looking for. I have a gridview and all I want is to hide one of the rows based on the value in the cell.
What I need to happen is something like in the logic of this :
if (row = "someValue")
{
row.Visible = false;
}
for the record, I have tried this but no luck:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string oRoleName = row.Field<string> ("SVal");
if (oRoleName.Equals ("someValue")) {
e.Row.Visible = false;
}
}
It is not base weather the row is the first the 2nd or third (like: e.row[1], e.row[2], etc.) I need to filter the data base on the value in the row. Can anyone teach me how could this be done ?
Would appreciate any help.
In a RowDataBound event add something along the lines of this logic
if (e.Row.Cells[5].Text == "foo") {
e.Row.Visible = false;
}
EDIT:
If youre looking to check the value of each row as its entered (unless im understanding incorrectly, you should probably expand on your question a bit.)
Then you may want to use the event "CellValueChanged"
Check to see if the cell is null beforehand and then do the check for your value and apply logic accordingly below that.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell != null) {
if (dataGridView1.CurrentCell.Value.ToString() == "foo")
{
// do your stuff here.
}
}
}
What you need to do is get the value of the column within the GridViewRow. You are on the right track. In RowDataBound, find the column you are after. Then check its value.
Here is an example. Use FindControl() to get the control in the specified column for the current row. If the control in that column is a Label, check the text of the label to see if it is the value you want hidden. If so, hide the row.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("MyLabel");
if (lbl.Text == "MyValue")
{
e.Row.Visible = false;
}
}
}

DataGridView cell is null on CellLeave

I am trying to do some text processing on the contents of the cell when the cell is left. I have the following code, but I get the following exception when I enter any text into the cell and then leave it.
An unhandled exception of type 'System.NullReferenceException' occurred in Program.exe
Additional information: Object reference not set to an instance of an object.
If I break and mousehover above .value it is indeed null, but I have entered data into the cell! So what gives?
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
string entry = "";
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
MakeTextFeet(entry);
}
if (e.ColumnIndex == 4)
{
string entry = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
MakeTextFeet(entry);
}
}
The value of a cell is in a transient state when the DataGridView CellLeave event fires. This is because DataGridView may be bound to a datasource and the change will not have been commited.
Your best option is to use the CellValueChanged event.
Add some checks:
DataGridViewCell MyCell = dataGridView1[e.ColumnIndex, e.RowIndex];
if (MyCell != null)
{
if (MyCell.Value != null)
{
}
}
I think you want to handle CellEndEdit rather than CellLeave.
On CellLeave, an edited cell's Value property is still unchanged (i.e. null for an empty cell as you observed by breaking and inspecting Value). On CellEndEdit, its new value has been set.
Try something like this, wherein I have tried to generally stick to your original code:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
if (e.ColumnIndex == 3 || e.ColumnIndex == 4)
{
string entry = "";
if (cell.Value != null)
{
entry = cell.Value.ToString();
}
MessageBox.Show(entry);
MakeTextFeet(entry);
}
}
I think you are leaving a blank cell and trying to process its value.
when you will leave a blank cell value of following code:
string entry = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
value in the string entry will be blank space(entry="")
and when you are processing this value further by passing it to another function[ MakeTextFeet(entry);] it is giving you error.
Solution from my point of view for this problem is>>>
put each line of code in above method also and MakeTextFeet(Entry) also in try block.
When you will write catch block leave that block empty.
Eg.
try
{
.
.
.
}
catch(Exception)
{
}
By this thing your exception will naturally get caught but since its nigligible, it will not show you.

how do i loop through each readlistviewitem, check the value, and set it to bold?

I have a listview that is loaded with a list of objects that contain an attribute called AssigneeView which holds the date that the entry was opened. The listview's ItemTemplate has a label named "lblHeader". What I want to do is loop through the ListView.Items and check each element's AssigneeView attribute, if it is null, I want to set the lblHeader.Text to be bold (indicating it is unread).
I want to create a method that takes an attribute from the Object in the Items list called ticketID and lookup whether or not the AssigneeView field is null for that field and return a bool. So it would look something like
ForEach item in listview.Items
if(IsUnread(item.datamember.ticketID)) then
item.lblHeader.MakeBold
else
item.lblHeader.MakeNotBold
I'm not 100% on how to dig into the telerik control to get what I need to do this. Any suggestions?
UPDATE:
here's where I am at the moment:
using (var client = new QUTIService.QSVCClient())
{
var data = client.SearchTickets(this.myGuid, txtSearchString.Text, 100, chkSearchClosed.Checked).ToList();
lsvResultTickets.DataSource = data;
lsvResultTickets.DataBind();
if (data.Count == 0)
{
lblStatus.Text = "No tickets found.";
}
else
{
foreach (var item in lsvResultTickets.Items)
{
var obj = item.DataItem as QT.FullTicket;
if (TicketIsUnread(obj.OriginalTicket.TicketID))
{
//???
}
}
}
}
What you will need to do is check AssigneeView in the RowDataBound event and then set lblHeader accordingly. RowDataBound is called for each row in you datasource as it is added to the GridView.
if (e.Row.RowType == DataControlRowType.DataRow) {
if (DataBinder.Eval(e.Row.DataItem, "AssigneeView") == null) {
//Set bold
} else {
//Set normal
}
}
Ok, it turns out that I just had to drill down one more level. I didn't need to pull out another method to do the check for me. I handled this is in the item loaded event handler, here's what I ended up with:
protected void ResultItem_DataBound(object sender, RadListViewItemEventArgs e)
{
var dItem = e.Item as RadListViewDataItem;
var dObj = dItem.DataItem as QT.FullTicket;
//if no read date, mark as unread (bold)
if (dObj.AssigneeView == null)
{
var headerLabel = e.Item.FindControl("lblHeader") as Label;
headerLabel.Style.Add("Font-Weight", "Bold");
headerLabel.Style.Add("Color", "Orange");
}
}

Categories

Resources