Using EPPlus, how can I write the actual excel TRUE value to a cell? I'm just getting the text value, and I have to explicitly click into the cell and press enter to have it recognize it as a boolean.
Cell.Value is an object so if you set it as a bool it will box it and preserve the type:
var cells = worksheet.Cells;
cells["A1"].Value = false; // not = "false"
cells["A2"].Value = true; // not = "true"
Which will give you this:
Related
I'd like to disable text boxes if the SponName combo box contains certain company names. I thought I could try a for loop for this, but I'm not sure if I created the right variables or used the for loop properly. Please help!
I've tried cobbling together the following code from the threads below but can't quite adapt it to my problem. I'm still learning VBA and am not great with declaring variables. There's no error message, but the text fields just won't disable after updating SponName.
VBA code for if like multiple criteria
Private Sub SponName_AfterUpdate()
Dim sponcontains As Variant
sponcontains = SponName.Text
Criteria = Array("Company1*", "Company2*", "Company3*", "Company24*")
For i = LBound(Criteria) To UBound(Criteria)
If InStr(sponcontains, Criteria(i)) > 0 Then
cps_manufsite_name.Enabled = False
cps_manufsite_city.Enabled = False
cps_manufsite_st.Enabled = False
cps_manufsite_ctry.Enabled = False
Else
cps_manufsite_name.Enabled = True
cps_manufsite_city.Enabled = True
cps_manufsite_st.Enabled = True
cps_manufsite_ctry.Enabled = True
End If
Next i
End Sub
Use Text property only when control still has focus. You need Value property which is default for data controls so don't have to reference. Explicitly declare all variables. Should have Option Explicit at top of all modules. Set the VBA editor > Tools > Options > Require Variable Declaration to automatically add to new modules - will have to type into existing. Use of * wildcard not appropriate with InStr() function - it's just another literal character.
Like this - default value for Boolean variable is False:
Option Compare Database
Option Explicit
___________________________________________________________________________________
Private Sub SponName_AfterUpdate()
Dim sponcontains As Variant, criteria As Variant, booE As Boolean, i As Integer
sponcontains = Me.SponName
criteria = Array("Company1", "Company2", "Company3", "Company24")
For i = LBound(criteria) To UBound(criteria)
If InStr(sponcontains, criteria(i)) > 0 Then
booE = True
Exit For
End If
Next
cps_manufsite_name.Enabled = Not booE
cps_manufsite_city.Enabled = Not booE
cps_manufsite_st.Enabled = Not booE
cps_manufsite_ctry.Enabled = Not booE
End Sub
But your solution without loop is just as valid and most likely shorter. Again, use Boolean variable to set Enabled state instead of duplicating.
booE = InStr("Company1,Company2,Company3,Company24", Me.SponName) > 0
Consider what would happen if you had to modify this list. Would have to modify code and distribute new db version to users. Alternatively, use a table with an attribute (can be a yes/no field) for company status. Code can do a lookup to table. Better to use company ID value to search. Your combobox should have that as a column in its RowSource and usually that would be the combobox's value.
booE = DLookup("Status", "Companies", "CompanyID = " & Me.SponName)
Use of Conditional Formatting can often avoid VBA altogether.
I am hoping to use the string value of a selected Listbox item in a .Match function within VBA - I need the the value '1' to be entered into the row where the value of the selection matches a value in column "A:A", on a specific column.
What I thought I would be able to do is to use a .value argument for the selected ListBox item, however this seems to either error out or give me a Boolean response, which isn't what I am after (I am after the actual string value of the item).
I have already looped through all items to set the Selected argument to True, and then I am looping through the list one by one to add '1' to the correct range.
Here is the code I thought would work (but doesn't, it throws an error of "Run-time error '13': Type mismatch" which is presumably down to the .Value not being a String.
For x = 0 To Me.CreditsEmployeesListBox.ListCount - 1
Me.CreditsEmployeesListBox.Selected(x) = True
Next
For i = 0 To Me.CreditsEmployeesListBox.ListCount - 1
If Me.CreditsEmployeesListBox.Selected(i) = True Then
employeeRow = WorksheetFunction.Match(Me.CreditsEmployeesListBox(i).Value, IndexSheet.Range("A:A"), 0)
IndexSheet.Range(Cells(employeeRow, showCodeColumn).Address).Value = 1
End If
Next
It errors out on the 'employeeRow = ...' line. Here, I am essentially trying to ask it:
employeeRow = WorksheetFunction.Match(<value of the currently referenced ListBox item>,IndexSheet.Range("A:A"),0)
Is this possible with VBA or am I going about this the wrong way?
Thanks
Matt
As an "hybrid" answer (as there is more than one problem) try this:
For x = 0 To Me.CreditsEmployeesListBox.ListCount - 1
Me.CreditsEmployeesListBox.Selected(x) = True
Next
Dim employeeRow As Variant
For i = 0 To Me.CreditsEmployeesListBox.ListCount - 1
If Me.CreditsEmployeesListBox.Selected(i) = True Then
employeeRow = Application.Match(Me.CreditsEmployeesListBox.List(i), IndexSheet.Columns(1), 0)
If IsNumeric(employeeRow) Then IndexSheet.Cells(employeeRow, showCodeColumn).Value = 1
End If
Next
This also should avoid VBA-errors.
If any questions are left, just ask :)
I know how to format cell values in EPPlus but only if the format applies to the whole value. How can I add formatting that only applies to part of the cell value?
for example...
Use the RichText collection to build it:
using (var pck = new ExcelPackage())
{
var wb = pck.Workbook;
var ws = wb.Worksheets.First();
var cell = ws.Cells["B2"];
cell.RichText.Add("This is some ");
cell.RichText.Add("formatting");
cell.RichText.Add(" withing a value");
cell.RichText[1].Color = Color.Red;
cell.RichText[1].Size = 14;
pck.Save();
}
At the moment, I've created a loop which runs through every cells in my datagridview to find if any cells are selected (highlighted), and if so that cell's row/ column header backcolor changes. The problem I have with this is that once the row and column count start to become a large amount, I start experiencing lag. So I wondering does anybody know a way of instantly being able to find whether a row or column contains a selected or highlighted cell without using a loop.
I was hoping that something like me.datagridview1.rows(1).selectedcells.count would exist, but haven't been able to find something like that, that works.
The other option (which I'd prefer not to do), is color the headercells as the selection is made.
Here is what I am using to find the selections. I have it looping through only the displayed cells to reduce the lag (which works), but I need the headercells colored, even when the selected cells are out of view (which then doesn't work).
a = .FirstDisplayedCell.RowIndex
Do While a < .FirstDisplayedCell.RowIndex + .DisplayedRowCount(True)
b = .FirstDisplayedCell.ColumnIndex
Do While b < .FirstDisplayedCell.ColumnIndex + .DisplayedColumnCount(True)
If .Rows(a).Cells(b).Selected = False Then
.Rows(a).HeaderCell.Style.BackColor = colorfieldheader
.Columns(b).HeaderCell.Style.BackColor = colorfieldheader
End If
b += 1
Loop
a += 1
Loop
a = .FirstDisplayedCell.RowIndex
Do While a < .FirstDisplayedCell.RowIndex + .DisplayedRowCount(True)
b = .FirstDisplayedCell.ColumnIndex
Do While b < .FirstDisplayedCell.ColumnIndex + .DisplayedColumnCount(True)
If .Rows(a).Cells(b).Selected = True Then
.Rows(a).HeaderCell.Style.BackColor = colorfieldheaderhighlight
.Columns(b).HeaderCell.Style.BackColor = colorfieldheaderhighlight
End If
b += 1
Loop
a += 1
Loop
You can do something like this C# code:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
dataGridView1.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
Ensure that the event handler is attached after data binding.
If you are highlighting the row same a selected cell (blue), you might want to set SelectionMode as FullRowSelect for the DataGridView. Like this:
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
place this code in your data grid view MouseDown event ...
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left )
{
this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
and for more info use how to highlight specific row in datagridview in c#
I am using Winforms datagridview (n-tier architecture) to populate from a dataset. I want to have a linkbutton as the last column which should change its text based on the value of two columns. While I have managed to get the linkbutton, I cannot get the value to change. I need the value to change so that when the linkbutton is clicked, it should open up different windows. My code is as below
Private Sub ShowProductRequisitionsInListView(ByVal data As DataSet, ByVal dgv As DataGridView)
dgvRequisitionDetails.Columns.Clear()
dgvRequisitionDetails.DataSource = Nothing
dgvRequisitionDetails.DataSource = data.Tables(0)
dgvRequisitionDetails.Columns(0).Width = 80
dgvRequisitionDetails.Columns(0).HeaderText = "Product Code"
dgvRequisitionDetails.Columns(1).Width = 180
dgvRequisitionDetails.Columns(1).HeaderText = "Product Name"
dgvRequisitionDetails.Columns(2).Width = 150
dgvRequisitionDetails.Columns(2).HeaderText = "Sales UOM"
dgvRequisitionDetails.Columns(3).Width = 60
dgvRequisitionDetails.Columns(3).HeaderText = "Qty. Reqd."
dgvRequisitionDetails.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRequisitionDetails.Columns(3).DefaultCellStyle.Format = "N3"
dgvRequisitionDetails.Columns(4).Width = 105
dgvRequisitionDetails.Columns(4).HeaderText = "Qty. In Stock"
dgvRequisitionDetails.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRequisitionDetails.Columns(4).DefaultCellStyle.Format = "N3"
Dim lnk As DataGridViewLinkColumn = New DataGridViewLinkColumn
dgvRequisitionDetails.Columns.Add(lnk)
lnk.HeaderText = "Action"
lnk.Text = "Click Here"
lnk.Name = "lnkAction"
lnk.UseColumnTextForLinkValue = True
End Sub
If the difference between the QtyReqd and QtyInStock is a negative value, the link button should read as "Not Available" and if there is enough stock it should read as "Available". Based on these two values different windows will open upon clicking the link
I tried to check the condition in the DataBindingComplete event, but its not working. What am I doing wrong here?
CL
I think that the easiest way is to add a computed column to the data table and use this as the data bound item for the link column. Something like this:
Dim expr As String = "IFF((([QtyInStock] - [QtyReqd]) >= 0), 'Available', 'Not Available')"
data.Tables(0).Columns.Add("Action", GetType(String), expr)
In the datagrid designer add a handler for: OnRowDataBound="someEvent_RowDataBound"
In the code behind you will have a function:
protected void someEvent_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
If(e.Row.RowType == DataControlRowType.DataRow){
((linkbutton)e.Row.Cells[someindex].controls(0)).Text = "somevalue";
}
}