Highlight button if field is not null C# - c#

I was hoping someone could share some code relative to "flagging" rows in a gridview. The first column of my gv is a column of buttons that bring up a new window based on the item listed in that row.
I would like to highlight the button (or the row in some other way) if one of two conditions (or both) is true.
The window pop-up that opens with the button click shows a small gv if data exists as well as a Comments formview. So I would highlight if that gv appears (meaning the data exists--query for this is already written) or if the Comments field is not null.
Little tricky since the two conditions work off different SPs and different source tables, but getting a highlight for at least one would still be a huge step.
Thank you so much for your help!

You can do it in the RowDataBound event of the Gridview. If a certain condition is met, then you can change the color of that row to distinguish it from other rows. For example:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
if(dr["ColumnName"] && dr["ColumnName1"])
{
e.Row.Style.Add("Color", "Red");
}
}
}

Related

Show the selected row record in textbox/label

How do I make the selected row record from a DataGridView show in a TextBox? I got some TextBox and Label in a Form. I want the text inside the TextBox/Label to change when the user selects a row record from the DataGridView. I tried the following code to make it happen, but it doesn't work
private void ItemTable_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
label_itemid_show.Text = ItemTable.Rows[e.RowIndex].Cells[0].Value.ToString();
text_itemname.Text = ItemTable.Rows[e.RowIndex].Cells[1].Value.ToString();
text_itemprice.Text = ItemTable.Rows[e.RowIndex].Cells[2].Value.ToString();
text_itemstock.Text = ItemTable.Rows[e.RowIndex].Cells[3].Value.ToString();
}
I'm working on a project that includes a features similar to yours. Currently I select a record in a datagridview and need to show it's values to textboxes so that any values can be edited.
This is how I tackle the problem.
Models.Item item = DAL.ItemDAL.GetItem(Convert.ToInt32(ItemDataGrid.CurrentRow.Cells[0].Value));
tbModifyItemID.Text = Convert.ToString(item.ItemID);
tbModifyItemName.Text = item.ItemName;
It's the ItemDataGridView.CurrentRow.Cells[0].Value that finds me the ItemID of the selected record.
In your case it's [DataGridName].CurrentRow.Cells[FieldNo, starting at 0].Value.ToString();
This allows you to click on any cell in the record and still retrieve the specified cell in the code.
Hope it helps c:
PS:
If you put it inside a
private void ItemDataGrid_SelectionChanged(object sender, EventArgs e)
method it will run the code anytime you click a different cell in the datagridview.
You can use the following:
lblDetails.Text = dgMainGrid.SelectedRows[0].Cells[1].Value.ToString();
lblDetails: A label on the form.
dgMainGrid: DataGrivView on the form.
PS: Just make sure you select the row inside the datagrid and not any particular cell, row selecting is done by clicking on the left of your first column i.e. the row selector column which gets added at the run time in WinForms.

C# DataGridView Reentrant call issue

C# winforms
I have a DataGridView populated from a list of objects. The DataGridViewis set to use fullrowselect with multiselect off.
I have a button above the table that they can use to select the next row. I have a Checkbox above the table that they use to change data in one of the cells of the selected row. The button and the Checkbox both work independently without issue.
I'd like to make it so that when they check that Checkbox it changes the data in the selected row cell and then selects the next row in the table.
No matter how I go about this, I either get the reentrant call error immediately or when I later try to select that row again.
I have tried BeginInvoke, BackGroundWorker, using a timer to change the row selection, setting a variable to remember the row and changing the value after the row selection has changed or after the row has validated. Every attempt gives me the same error.
My code is much too long to post here, but I may be able to create a simple app to demonstrate the issue if needed.
Please help.
Here is the code:
private void chkDone_CheckedChanged(object sender, EventArgs e)
{
ProcedureCode proc = getCode(gridCodes.SelectedRows[0].Cells[3].Value + "");
proc.done = chkDone.Checked;
setPnlProgress();
if(chkDone.Checked)
{
btnNext_Click(sender, e);
}
}
private void btnNext_Click(object sender, EventArgs e)
{
int cRow = gridCodes.SelectedRows[0].Index;
if (cRow < gridCodes.RowCount - 1)
{
gridCodes.CurrentCell = gridCodes[0, cRow + 1];
}
}
Here is a screenshot of the table and checkbox:Click to see screenshot
The second checkbox labled Done is the one that changes the value in the cell of the selected row. The third circle button selects the next row.
I found that the the checkbox CheckChanged event was being fired when I went to the next row as well as when the user clicked the checkbox. I added a flag to skip any action during the row entering phase and this resolved the reentrant error.

Add a column in gridview at run time

I want to add a new column in grid view at run time and in this column I want to add a button in some specific rows. How can I do this?
Please suggest me some relevant solution as soon as possible.
Although you can programmatically add column fields to the Columns collection, it is easier to list the column fields declaratively in the GridView control and then use the Visible property of each column field to show or hide each column field.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns(v=vs.100).aspx
So you see what you should do is go ahead and create the column in your GridView, but set the Visible property to False. Then to control the display of the column, you can make use of the GridView.RowDataBound Event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.100).aspx
You should also be able to handle displaying a button from within the same RowDataBound handler.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(condition)
{
Button btn = (Button)e.Row.FindControl("ButtonID");
btn.Visible = false;
}
}
}

Datagridview checkbox column has a dead area

I have a datagridview (winforms) with a checkbox column as well as other text-based columns. I've successfully worked through most of the common issues around checkbox columns which are very well documented on this site.
However, I have 1 remaining problem. I am able to click "directly" on a checkbox and it does respond the way I want. However, if I carefully move the mouse pointer between the cell boundary and the checkbox control, and mouse click, I am able to select the cell but the state of the checkbox does not toggle. This problem is much more evident when the row height is bigger for a given row.
Thanks for any help
NOTE: this is not, I repeat NOT, the issue that occurs when focus moves off a given checkbox cell after it is checked. I have that one solved.
This is not an issue. This is simply how it is supposed to work. For a grid column you can have cellclick events and cellcontentclick events. Since I want the checkbox to check when I click anywhere inside the cell, I should use cellclick. Among other events you will need to listen for, I added the following to my code:
private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == 1) && e.RowIndex != -1)
{
this.MyGrid[1, e.RowIndex].Value = !(bool)this.MyGrid[1, e.RowIndex].Value;
this.MyGrid.EndEdit();
}
}

Textbox control in DataGridView

I am working on a datagridview in C# in windows application. I want to add textbox controls in DataGridView. So, when we run it then textbox should be shown in gridview and we can put value in it and My grid has 3 columns and I want to add new row in grid when I press tab on 3rd column of gridview.
How do I do this?
It's hard to provide a precise answer since your question is lacking in detail and pretty general, but to get textboxes in your DataGridView, you're going to want to add some instances of DataGridViewTextBoxColumn to the DataGridView's Columns collection. This will cause them to be populated with textboxes in each row.
To detect when the user presses tab on the 3rd column, you can add a 1-2 pixel wide fourth column and detect that it has recieved focus (almost definitely from a tab keystroke) using the OnCellEnter event.
Good luck!
So, for the "display textboxes by default portion of your question, here's the skinny:
On GridView->Edit Columns, add the columns you want to use explicitly. Then click on the link "Convert this field into a templateField". This will let you tweak the generated HTML for those cells. Say OK. Then go to GridView->Edit Templates. For your favorite Column, copy the ItemEditTemplate into the ItemTemplate. (ItemTemplate is the default. ItemEditTemplate contains the properly bound edit control.) Now all of your data fields will default to "editable."
I'm guessing you have a submit button. You'll need to tell the GridView to update the rows on submit., like so:
For Each r As GridViewRow In GridView1.Rows
Dim mon = System.Int32.Parse(CType(r.FindControl("TextBox1"), TextBox).Text)
If mon <> 0 Then GridView1.UpdateRow(r.RowIndex, False)
Next
Obviously, you'll want different logic inside there, but the basic loop/findControl/updateRow logic should apply.
Microsoft has a walkthrough on this here: Performing Bulk Updates to Rows Bound to a GridView
Try this, for example if you want to set your first column in datagridview as a textbox control:
private void dtgrdview_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
TextBox txtbox1=new TextBox();
dtgrdview.Controls.Add(txtbox1);
Rectangle rectangle = dtgrdview.GetCellDisplayRectangle(0, e.RowIndex, true);
txtbox1.Location = rectangle.Location;
txtbox1.Size = rectangle.Size;
txtbox1.TextChanged += txtbox1_TextChanged;
txtbox1.Leave += txtbox1_Leave;
txtbox1.Visible = true;
}
}
and don't forget to add this event in the same class as like below to call it when the cell have the focus:
private void txtbox1_Leave(object sender, EventArgs e)
{
txtbox1.Visible = false;
}
private void txtbox1_TextChanged(object sender, EventArgs e)
{
dtgrdview.CurrentCell.Value = txtbox1.Text;
}
If you have any other questions, don't hesitate to ask me :)

Categories