Changing Cell Values of DataGridView - linq-to-sql

Suppose I have the following hypothetical table:
How can I make a DataGridView display the following?
Notice the values of Sick have changed.
I have tried the following to no avail:
var query = from c in Patients
select new
{
c.Name,
c.Sick == 1 ? "Yes" : "No"
};

You can use the DataGridView.CellFormatting event.
private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Sick")
{
if (e.Value != null)
{
if (e.Value.ToString() == "1"
{
e.Value = "Yes";
}
else
{
e.Value = "No";
}
e.FormattingApplied = true;
}
}
}

Related

AngularJS Selects Empty Option Even Valid Option is Avaliable

I'm using AngularJS ver. 1.2.15 on my project. And, I have a select element on one of my views as per below:
<select class="select-white form-control form-select" id="cat2_{{feed.id}}" ng-model="feed.operationstatusid" ng-change="updateCategoryAndStatus(feed, true)"></select>
And, I'm feeding this element like this:
function SetCategory2(cat1Id, feed) {
var feedId = feed.id;
var fromRuleOpStatusId = -1;
$('#cat2_' + feedId).find('option').remove();
if (cat1Id > -1) {
$('#cat2_' + feedId).append($('<option></option>').text(lang.SelectSubCategory).val(0));
$.each($scope.category2, function (index, cat2Item) {
$('#cat2_' + feedId).append($('<option></option>').text(cat2Item.statusdescription).val(cat2Item.id));
});
var isselected = false;
$.each($scope.category2, function (index, cat2Item) {
if (feed.operationstatusid == cat2Item.id) {
$('#cat2_' + feedId).val(cat2Item.id);
fromRuleOpStatusId = -1;
isselected = true;
}
else {
var feedStr = "";
if (feed.title != undefined && feed.title != null) {
feedStr = feed.title.toLowerCase();
}
if ($scope.catTitleRulesTwo) {
$.each($scope.catTitleRulesTwo, function (r_index, r_item) {
if (cat2Item.id == r_item.titleCode && !isselected) {
if (feedStr != undefined && feedStr != null && r_item != undefined && r_item != null) {
String.prototype.contains = function (str) { return this.toLowerCase().indexOf(str) !== -1; };
var text = feedStr;
if (eval(r_item.ruleexpression)) {
$('#cat2_' + feedId).val(cat2Item.id);
fromRuleOpStatusId = cat2Item.id;
isselected = true;
}
}
}
});
}
}
});
if (fromRuleOpStatusId != -1) {
feed.operationstatusid = fromRuleOpStatusId;
}
}
else {
$('#cat2_' + feedId).append($('<option></option>').text(lang.SelectSubCategory).val(0));
}
}
I am aware of the facts about eval function, but the project I'm working on is quite old, so does the code. Anyway, this is about business logic and quite irrelevant with the thing I'm going to ask (or so I was thinking).
As you can see I'm appending all the options before I set the value of the selectbox with using .val(...). I have also checked that values do match along with the data types. But, when I observe this function step by step, I saw that selected value does show up without flaw. After the code finish with my above mentioned function (SetCategory2), code goes through on of the function located on AngularJS file, named xhr.onreadystatechange. It's not a long function, so I'm sharing it also on below.
xhr.onreadystatechange = function() {
if (xhr && xhr.readyState == 4) {
var responseHeaders = null,
response = null;
if(status !== ABORTED) {
responseHeaders = xhr.getAllResponseHeaders();
response = ('response' in xhr) ? xhr.response : xhr.responseText;
}
completeRequest(callback,
status || xhr.status,
response,
responseHeaders);
}
};
After the code released from this function, respective selectbox's value is pointed at the empty option.
I have run into topics which talks about this behaviour might due to invalid option-value match, but as I described above, I append all my options before deciding the value. So, I can't figure out what I'm missing.
Thank you in advance.

Issue with setValues

I have this code :
function calculateTotals(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var total, clientID,cellValues,cellFontLines, result;
for (i=0;i<98;i++) {
//sheet.getRange("K"+i).setValue("calculating");
clientID=sheet.getRange("J3:J100").getValues();
total=0;
cellValues=sheet.getRange("B3:H234").getValues();
cellFontLines=sheet.getRange("B3:H234").getFontLines();
result=sheet.getRange("K3:K100").getValues(); // this is dumb
if (clientID[i][0] != "") {
for (j=0;j<=6;j++) {
for (k=0;k<=231;k++) {
if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
total++;
}
}
}
result[i][0]=total/2;
Logger.log(i,clientID[i][0],result[i][0]);
//sheet.getRange("K"+(i+3)).setValue(result[i][0]);
} else {
break;
}
}
sheet.getRange("K3:K100").setValues(result); // this doesn't seem to do anything
Logger.log(result);
}
I can't figure out how to use setValues. I logged it all, and it looks right, in the debugger the array is all set but I still have to use the commented out setValue() line to update that column.
As an aside, is there a better way to initialize result to the correct dimensions than just reading in the previous results?
This works :
function calculateTotals(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var total, clientID,cellValues,cellFontLines, result=new Array(98);
clientID=sheet.getRange("J3:J100").getValues();
cellValues=sheet.getRange("B3:H234").getValues();
cellFontLines=sheet.getRange("B3:H234").getFontLines();
result=sheet.getRange("K3:K100").getValues();
for (i=0;i<98;i++) {
//sheet.getRange("K"+i).setValue("calculating");
total=0
if (clientID[i][0] != "") {
for (j=0;j<=6;j++) {
for (k=0;k<=231;k++) {
if (cellValues[k][j] == clientID[i][0] && cellFontLines[k][j] != 'line-through' ) {
total++;
}
}
}
result[i][0]=total/2;
} else {
break;
}
}
sheet.getRange("K3:K100").setValues(result);
}

Angular: 2 bugs in saving to local storage

I have 2 bugs in methods that save products to local storage (when a user adds them to 'favorites').
The code is part of Angular service, but it can be read without this reference also.
First bug: Sometimes, when a user saves a product to favorites, not one, but multiple products get stored.
Second bug: When I store too many products, I get an error saying that the storage is full (it's full because I parse JSON wrongly, so unwanted additional backslashes are added).
This is the codepan (you can see the result in the console): https://codepen.io/mandy555/pen/dyPVGxj
storage;
storageKeys;
x;
i = 0;
saveId;
arr;
normalArr;
productInStore;
productsInStore;
idsInStore;
newIds;
newProducts;
favorites;
storageAvailable(type) {
try {
this.storage = window[type];
this.x = '__storage_test__';
this.storage.setItem('x', this.x);
this.storage.removeItem('x');
return true;
}
catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(this.storage && this.storage.length !== 0);
}
}
storeFavorite(product = '', productId = '') {
productId = String(productId);
this.normalArr = [];
if (this.storageAvailable('localStorage')) {
this.storage = window.localStorage;
if (!this.storage.getItem('favorites_id')) {
this.saveId = productId;
this.normalArr.push(product);
} else {
this.productsInStore = JSON.parse(this.storage.getItem('favorites_product'));
if (this.storage.getItem('favorites_id').indexOf(',') === -1) {
this.arr = [this.storage.getItem('favorites_id')];
this.normalArr = this.productsInStore;
} else {
this.arr = this.storage.getItem('favorites_id').split(',');
this.normalArr = this.productsInStore;
}
if (!this.arr.includes(productId) && productId !== '') {
this.saveId = this.storage.getItem('favorites_id') + ',' + productId;
this.normalArr.push(product);
} else {
this.saveId = this.storage.getItem('favorites_id');
}
}
this.storage.setItem('favorites_id', this.saveId);
this.storage.setItem('favorites_product', JSON.stringify(this.normalArr));
} else {
this.storage = null;
}
}
deleteFavorite(productId = '') {
productId = String(productId);
this.normalArr = [];
if (this.storageAvailable('localStorage')) {
this.storage = window.localStorage;
if (this.storage.getItem('favorites_id') && this.storage.getItem('favorites_product')) {
this.productsInStore = JSON.parse(this.storage.getItem('favorites_product'));
if (this.storage.getItem('favorites_id').indexOf(',') === -1) {
this.storage.clear();
} else {
this.idsInStore = this.storage.getItem('favorites_id').split(',');
this.newIds = this.idsInStore.filter(id => {
return id !== productId;
});
this.newproducts = this.productsInStore.filter(product => {
return String(product.id) !== productId;
});
this.newIds = this.newIds.join();
this.storage.setItem('favorites_id', JSON.stringify(this.newIds));
this.storage.setItem('favorites_product', JSON.stringify(this.newproducts));
}
}
} else {
this.storage = null;
}
}
The first problem was that I forgot to assign storage to local storage. The second solution was to save x and not JSON.stringify(x) in the local storage, since x was already a string. The corrected version is here:
https://codepen.io/mandy555/pen/dyPVGxj

as3 - Changing position checking for in IF statement

I am trying to catch X.Y positions with an IF statement, and IF the coordinates are true I want it to go on to the next set of coordinates. In the code below I attempted my best but I keep getting the "Cannot assign to a non-reference value." Error.
public function onDd(event:TimerEvent):void
{
if (this.rootClass.world.strMapName == "test" && a1.x = a1.x = 327.1 && a1.y = 249.65)
{
a1.x = 360.7;
a1.y = 204.25;
}
else if (a1.x = 410.15 && a1.y = 204.25)
{
a1.x = 360.7;
a1.y = 204.25;
}
}
You have used a wrong comparison operator
If you want to compare two values you must use == or ===
So your code will become:
public function onDd(event:TimerEvent):void {
if (this.rootClass.world.strMapName == "test" && a1.x == 327.1 && a1.y == 249.65) {
a1.x = 360.7;
a1.y = 204.25;
}
else if (a1.x == 410.15 && a1.y == 204.25) {
a1.x = 360.7;
a1.y = 204.25;
}
}

Unable to use drag Drop in radtreeview

I have binded all data to RadTreeView but unable to use drag-'n-drop. I used four properties as
IsDragDropEnabled="True"
IsDropPreviewLineEnabled="True"
AllowDrop="True"
IsDragPreviewEnabled="True"
and I want to drop an item within same tree. But it doesnt work.
There is quite a bit of info here : http://www.telerik.com/help/silverlight/raddraganddrop-events.html
But I'm also having trouble with the tree view.
After a quick read of this telerik article, drag-drop seems to be working quite well for me.
<telerik:RadTreeView ... EnableDragAndDrop="true" OnNodeDrop="MyTreeView_NodeDrop">
EnableDragAndDrop and OnNodeDrop seem to be the two vital pieces to getting it working, but weren't in your list of attributes you tried. HTH
<telerik:RadTreeView x:Name="treeView1" IsDragDropEnabled="True" Margin="2,0,0,0" ItemsSource="{Binding SelectedSectionList, Mode=TwoWay}" ItemTemplate="{StaticResource SectionTemplate}" IsEditable="True" SelectedItem="{Binding SelectedCustomSectionList, Mode=TwoWay}" Grid.Column="2">
Now in the code behind
You have to fire event
In Constructor
this.treeView1.AddHandler(RadDragAndDropManager.DropQueryEvent, new EventHandler<DragDropQueryEventArgs>(OnDropQuery), true);
Then
private void OnDropQuery(object sender, DragDropQueryEventArgs e)
{
RadTreeViewItem destinationItem = e.Options.Destination as RadTreeViewItem;
object source = this.GetItemFromPayload<object>(e.Options.Payload);
object target = destinationItem != null ? destinationItem.Item : null;
DropPosition position = destinationItem != null ? destinationItem.DropPosition : DropPosition.Inside;
if (source != null && target != null)
{
Section sourceSection = source as Section;
Section targetSection = target as Section;
Question sourceQuestion = source as Question;
Question targetQuestion = target as Question;
if (sourceQuestion != null)
{
try
{
if (sourceQuestion != null && targetQuestion != null && object.ReferenceEquals(sourceQuestion, targetQuestion))
{
sourceSection.Questions.Remove(sourceQuestion);
targetSection.Questions.Add(sourceQuestion);
e.QueryResult = false;
return;
}
if (targetQuestion != null && position == DropPosition.Inside)
{
sourceSection.Questions.Remove(sourceQuestion);
targetSection.Questions.Add(sourceQuestion);
e.QueryResult = false;
return;
}
if (position != DropPosition.Inside && targetQuestion == null)
{
sourceSection.Questions.Remove(sourceQuestion);
targetSection.Questions.Add(sourceQuestion);
e.QueryResult = false;
return;
}
}
catch (Exception ex)
{
}
}
}
else
{
e.QueryResult = false;
return;
}
e.QueryResult = true;
}
This is it. You will be able to drag and drop.