i've this code to insert in DB the data from the "hours1, hours2, hours3" that cames from an excel import, but this makes the code bigger by the way im doing, this is my code, how can i simplfy the part of the hours1,hours2 == nulls.. ?
switch (colNames.IndexOf(item2))
{
case 0:
if (model.Hours == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours;
item.Hours_Remaining = (decimal)model.Hours;
}
break;
case 1:
if (model.Hours1 == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours1;
item.Hours_Remaining = (decimal)model.Hours;
}
break;
case 2:
if (model.Hours2 == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours2;
item.Hours_Remaining = (decimal)model.Hours;
}
break;
case 3:
if (model.Hours == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)model.Hours3;
item.Hours_Remaining = (decimal)model.Hours;
}
break;
}
you can have a private method like this
private void NameOfPrivateMethod(object objValue,Hours hours,Item item)
{
if (objValue == null)
{
item.Hours = 0;
}
else
{
item.Hours = (decimal)objValue;
item.Hours_Remaining = (decimal)hours;
}
}
and use it like this (you need to make some changes to it according to your need)
NameOfPrivateMethod(model.Hours1, model.Hours,Item)
Related
I am trying to do something like if there is a selection from a drop downbox it should make a change in color to the Datagridview row.
I have used switch case, doent change nothing, hence I wanted to use another approach to it. Code looks like this
private void RowsColor()
{
try
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1 != null)
{
if (dataGridView1.Rows != null)
{
if (dataGridView1.Rows[i].Cells != null)
{
if (dataGridView1.Rows[i].Cells[11] != null)
{
//string val = dataGridView1.Rows[i].Cells[11].Value.ToString();
var val = dataGridView1.Rows[i].Cells[11];
if (val = "Confirm Appointment")
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
else if (val = "Reschedule")
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Now I am getting something like this as an error
cannot implicitly convert type string to
System.Windows.Forms.Datagridviewcell
New to this, What exactly am I missing?
Decomment
string val = "";
try{
val = dataGridView1.Rows[i].Cells[11].Value.ToString();
}
catch(NullReferenceException ex){
val = "Not yet set"
}
and
if (val.Equals("Confirm Appointment"))
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
else if (val.Equals( "Reschedule"))
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
From a CompactFramework, I got a TypeString which can be a complex type, meaning it can use generics, arrays, etc. That means it can look like Sytem.Tuple’1[[Sytem.String, mscorlib]]. Problem is, I can not use Type.GetType because the assemblys in the Typestring may be wrong or not present.
Is there a lib which parses the string, and returns a type by searching all types in appdomain for the right one?
I've now found no Code, but solved using this:
public static class TypeHelper
{
public static Type GetTypeFromString(string typeString)
{
int pos = 0;
return ParseInternal(typeString, ref pos);
}
private static Type ParseInternal(string name, ref int pos)
{
StringBuilder sb = new StringBuilder();
List<Type> genericParameters = null;
int arrayDimensions = 0;
bool ignore = false;
while (pos < name.Length)
{
char c = name[pos++];
switch (c)
{
case ',':
{
if (arrayDimensions > 0)
arrayDimensions++;
else
ignore = true;
break;
}
case '[':
{
if (name[pos] == '[')
{
if (genericParameters == null)
genericParameters = new List<Type>();
pos++;
genericParameters.Add(ParseInternal(name, ref pos));
}
else if (genericParameters!=null)
genericParameters.Add(ParseInternal(name, ref pos));
else
arrayDimensions++;
break;
}
case ']':
{
var currentName = sb.ToString();
var type = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).FirstOrDefault(x => x.FullName == currentName);
if (name.Length > pos && name[pos] == ']')
{
pos++;
}
if (genericParameters != null)
{
return type.MakeGenericType(genericParameters.ToArray());
}
else if (arrayDimensions != 0)
{
return type.MakeArrayType(arrayDimensions);
}
return type;
}
default:
if (!ignore)
sb.Append(c);
continue;
}
}
{
var currentName = sb.ToString();
var type = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).FirstOrDefault(x => x.FullName == currentName);
if (genericParameters != null)
{
return type.MakeGenericType(genericParameters.ToArray());
}
else if (arrayDimensions != 0)
{
return type.MakeArrayType(arrayDimensions);
}
return type;
}
}
}
If from date is selected in calendar Extender, I need to clear previous dates than from date from To Date Calendar as shown in figure. Please provide your comments
Use this CSS property on your any .css class
.ajax__calendar_day_disabled
{
background-color:#ccc !important;
color:#eee !important;
}
use above css class in date textbox like CssClass="disable_past_dates"
and use script
function getElementsByClassName(a, b) { var c = []; var d = a.getElementsByTagName("*"); for (var e = 0; e < d.length; e++) { if (d[e].className.indexOf(b) != -1) { c.push(d[e]) } } return c } function DisableDates(a) { if (a._element.className.indexOf("disable_past_dates") != -1) { var b = getElementsByClassName(a._days, "ajax__calendar_day"); var c = (new Date).setHours(0, 0, 0, 0); for (var d = 0; d < b.length; d++) { try { if (b[d].date.setHours(0, 0, 0, 0) >= c) { b[d].className = "ajax__calendar_day" } else { b[d].className = "ajax__calendar_day ajax__calendar_day_disabled" } } catch (e) { } } } if (a._element.className.indexOf("disable_future_dates") != -1) { var b = getElementsByClassName(a._days, "ajax__calendar_day"); var c = (new Date).setHours(0, 0, 0, 0); for (var d = 0; d < b.length; d++) { try { if (b[d].date.setHours(0, 0, 0, 0) <= c) { b[d].className = "ajax__calendar_day" } else { b[d].className = "ajax__calendar_day ajax__calendar_day_disabled" } } catch (e) { } } } } AjaxControlToolkit.CalendarBehavior.prototype.show = function (a) { this._ensureCalendar(); if (!this._isOpen) { var b = new Sys.CancelEventArgs; this.raiseShowing(b); if (b.get_cancel()) { return } this._isOpen = true; this._popupBehavior.show(); if (this._firstPopUp) { this._switchMonth(null, true); switch (this._defaultView) { case AjaxControlToolkit.CalendarDefaultView.Months: this._switchMode("months", true); break; case AjaxControlToolkit.CalendarDefaultView.Years: this._switchMode("years", true); break } this._firstPopUp = false } this.raiseShown(); DisableDates(this) } }; AjaxControlToolkit.CalendarBehavior.prototype._cell_onclick = function (a) { a.stopPropagation(); a.preventDefault(); if (!this._enabled) return; var b = a.target; var c = this._getEffectiveVisibleDate(); Sys.UI.DomElement.removeCssClass(b.parentNode, "ajax__calendar_hover"); switch (b.mode) { case "prev": case "next": this._switchMonth(b.date); break; case "title": switch (this._mode) { case "days": this._switchMode("months"); break; case "months": this._switchMode("years"); break } break; case "month": if (b.month == c.getMonth()) { this._switchMode("days") } else { this._visibleDate = b.date; this._switchMode("days") } break; case "year": if (b.date.getFullYear() == c.getFullYear()) { this._switchMode("months") } else { this._visibleDate = b.date; this._switchMode("months") } break; case "day": if (this._element.className.indexOf("disable_past_dates") != -1) { if (b.date.setHours(0, 0, 0, 0) >= (new Date).setHours(0, 0, 0, 0)) { this.set_selectedDate(b.date); this._switchMonth(b.date); this._blur.post(true); this.raiseDateSelectionChanged() } } else if (this._element.className.indexOf("disable_future_dates") != -1) { if (b.date.setHours(0, 0, 0, 0) <= (new Date).setHours(0, 0, 0, 0)) { this.set_selectedDate(b.date); this._switchMonth(b.date); this._blur.post(true); this.raiseDateSelectionChanged() } } else { this.set_selectedDate(b.date); this._switchMonth(b.date); this._blur.post(true); this.raiseDateSelectionChanged() } break; case "today": this.set_selectedDate(b.date); this._switchMonth(b.date); this._blur.post(true); this.raiseDateSelectionChanged(); break } DisableDates(this) };
This will disabled all the past dates but not clears it.
try this one,
CntrlID.MinDate = DateTime.Now;
CntrlID.MaxDate = DateTime.Today.AddMonths(12);
CntrlID.MaxDate = DateTime.Today.AddYears(100);
I want to know if a Word Document has page number in the Header/Footer using Interop.Word in C#.
I have gone this far
try
{
foreach (Section SectionObj in DocObj.Sections)
{
foreach (HeaderFooter HeaderObj in SectionObj.Footers)
{
if (HeaderObj.Exists && (HeaderObj.Shapes.Count > 0 || !(string.IsNullOrEmpty(HeaderObj.Range.Text) || HeaderObj.Range.Text.Equals("\r"))))
{
if(HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleNumberInDash
|| HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleUppercaseLetter
|| HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleUppercaseRoman
|| HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleNumberInCircle
|| HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleLowercaseRoman
|| HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleLowercaseLetter
|| HeaderObj.PageNumbers.NumberStyle == WdPageNumberStyle.wdPageNumberStyleHindiLetter1)
{
PageNumber = true;
}
}
}
}
}
catch (Exception e) { }
But the checks cannot determine if it is Page number or not. What should I do to determine if it has page numbers?
You should check the Header and the Footer of Section using the PageNumbers.Count propertie.
private bool HeaderOrFooterHasPageNumber(Word.HeaderFooter headOrFooter)
{
if (headOrFooter != null)
return headOrFooter.PageNumbers.Count > 0;
else
return false;
}
And you can call this like...
bool hasNumberPages = false;
Word.Document doc = WordApp.ActiveDocument;
try
{
Word.HeaderFooter headOrFooter = null;
Word.Section section = null;
for (int i = 1; i <= doc.Sections.Count; i++)
{
try
{
section = doc.Sections[i];
if(section != null)
{
headOrFooter = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
if (hasNumberPages)
break;
headOrFooter = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages];
hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
if (hasNumberPages)
break;
headOrFooter = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage];
hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
if (hasNumberPages)
break;
headOrFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
if (hasNumberPages)
break;
headOrFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages];
hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
if (hasNumberPages)
break;
headOrFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage];
hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
if (hasNumberPages)
break;
}
}
finally
{
if(headOrFooter != null)
{
Marshal.ReleaseComObject(headOrFooter);
headOrFooter = null;
}
if (section != null)
Marshal.ReleaseComObject(section);
}
}
}
finally
{
if (doc != null)
Marshal.ReleaseComObject(doc);
}
Is there a better way to pass this through the addClick (ideally I don't want this at all and I would want it to auto-pass through)?
public void addClick(object sender, EventArgs e)
{
if ((string) HttpContext.Current.Session["whichMenu"] == "systemDateFormats")
{
WorldViewNet.system.DateFormats dateformats = new WorldViewNet.system.DateFormats();
dateformats.addClick();
}
else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingLabels")
{
WorldViewNet.programming.Labels labels = new WorldViewNet.programming.Labels();
labels.addClick();
}
else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingPLUSearch")
{
WorldViewNet.programming.PLUSearch pluSearch = new WorldViewNet.programming.PLUSearch();
pluSearch.addClick();
}
else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingServings")
{
WorldViewNet.programming.Servings servings = new WorldViewNet.programming.Servings();
servings.addClick();
}
else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingShops")
{
WorldViewNet.programming.Shops shops = new WorldViewNet.programming.Shops();
shops.addClick();
}
else if ((string) HttpContext.Current.Session["whichMenu"] == "programmingTextsSearch")
{
WorldViewNet.programming.TextsSearch textsSearch = new WorldViewNet.programming.TextsSearch();
textsSearch.addClick();
}
else if ((string) HttpContext.Current.Session["whichMenu"] == "systemTemplates")
{
WorldViewNet.system.Templates templates = new WorldViewNet.system.Templates();
templates.addClick();
}
}
If anyone has any suggestions, that would help me out I would be grateful.
I thing below models maybe good for your codes:
public void addClick(object sender, EventArgs e)
{
object control;
string opt = (string) HttpContext.Current.Session["whichMenu"];
switch (opt)
{
case "systemDateFormats": control = new WorldViewNet.system.DateFormats();
break;
case "programmingLabels": control = new WorldViewNet.programming.Labels();
break;
case "programmingPLUSearch": control = new WorldViewNet.programming.PLUSearch();
break;
case "programmingServings": control = new WorldViewNet.programming.Servings();
break;
case "programmingShops": control = new WorldViewNet.programming.Shops();
break;
case "programmingTextsSearch": control = new WorldViewNet.programming.TextsSearch();
break;
case "systemTemplates": control = new WorldViewNet.system.Templates();
break;
default: new WorldViewNet.system.DefaultType();
}
((dynamic)control).addClick();
}