Linq to strikeout Telerik datagrid row - c#

I am using asp.net 3.5 with c#. I am getting records in dataset that contains a field with name IsDeleted. Now for this field, if it is set to true then I want to strikeout the entire row's text.
For example : I am making Capital Case the text of field Alert using linq code below :
ds.Tables[0].AsEnumerable().ToList().ForEach(i => i["Alert"] = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(i["Alert"].ToString()));
Something like this is possible to strikeout the entire row's text?
Thanks

Obviously TextInfo doesn't have any method to do such modification, since it's applied in HTML only.
You can however add the HTML text-decoration to every cell.
Try this:
ds.Tables[0].AsEnumerable().ToList()
.Where(i=>i["IsDeleted"].ToString().Equals("True"))
.ForEach(i =>
{
i["Alert"] = "<span style='text-decoration:line-through;'>" +
i["Alert"].ToString() +
"</span>";
i["Name"] = "<span style='text-decoration:line-through;'>" +
i["Name"].ToString() +
"</span>";
i["Date"] = "<span style='text-decoration:line-through;'>" +
i["Date"].ToString() +
"</span>";
});

Related

C# Updating multiple text boxes with similar names

I have multiple textbox names such as R1TotalCost, R2TotalCost, R3TotalCost, all the way up to R25TotalCost. Is there anyway to edit the text values, and or text colours to them all using a code simular to this
for (i=1; i <=25, i++) {
string TextBoxName ="R" + i + "TotalCost";
TextBoxName.text = "£25";
TextBoxName.Foreground = Brushes.Green;
}
I think the best way to go about this since you seem to want to update them all at once, would be to create an Array or List that contains all of the TextBox elements. Then your can go through them all like below!
foreach (TextBox tb in myTextBoxes) {
tb.Content = "UPDATED CONTENT!";
}

Convert dataset into combobox items using LINQ

I am trying to take a DataSet and add each item to a ComboBox.
I am currently using a foreach loop, like so:
foreach (DataRow row in ds.Tables[0].Rows)
{
cmbCauseForRepair.Items.Add(row[0].ToString() + ":" + row[1].ToString());
}
I would like to do this using LINQ.
Here is what I'm trying:
cmbCauseForRepair.Items.Add(from r in ds.Tables[0].Rows.Cast<DataRow>()
select r[0] + ":" + r[1]);
However, my ComboBox only has 1 item: "System.Linq.Enumerable".
LINQ isn't looping over the records for you. You still need to do that.
If cmbCauseForRepair.Items.Add() had an overload which accepted an enumeration of values then you wouldn't need to. But it doesn't. It just accepts an object. And according to that documentation, that object will be treated as:
A visual representation of the item is displayed in the combo box. This content representation is specified by the DisplayMember property. If the DisplayMember property is null, the item's ToString method is called to obtain the string that is displayed in the combo box; otherwise, the property of the stored object as specified by the DisplayMember property is displayed.
Since the object being passed to Add() is of type IEnumerable<string> then the .ToString() representation of it is:
`IEnumerable<string>`
Basically, you need to loop through your objects to add them one at a time:
var items = from r in ds.Tables[0].Rows.Cast<DataRow>()
select r[0] + ":" + r[1];
foreach (var item in items)
cmbCauseForRepair.Items.Add(item);
Or use a different method to add them:
cmbCauseForRepair.Items.AddRange(from r in ds.Tables[0].Rows.Cast<DataRow>()
select r[0] + ":" + r[1]);
.Add() only adds a single item.
Try the same approach, but use .AddRange(), which adds a collection of Objects to the ComboBox:
cmbCauseForRepair.Items.AddRange(from r in ds.Tables[0].Rows.Cast<DataRow>()
select r[0] + ":" + r[1]);
Better to use string.Format instead of concatenation string
cmbCauseForRepair.Items.AddRange(ds.Tables[0].Rows.Cast<DataRow>().Select(p => string.Format("{0}:{1}", p[0], p[1])).ToArray());

grid view search using dropdown list and text box filter?

Sir/madam now my problem is this that I want to filter the Grid View of a page using a Drop Down list and a text box.
I mean to say like we write a SQL such as:
Select * from student where roll_no = 101;
Right,
Now I what that the column (roll_no in above statement) should be selected by the drop down list and the value (101 in the above statement) should be entered by the Text box.
In short I want to populate my grid view using Drop Down list and the value of text box by clicking a button..
For developing i am using dataset and table adapters.
Please, help me for this..
I use a drop-down list (combo-box) and a textbox to filter my DataGridView the following way and I think this is what you are looking for.
First, populate your DataGridView. You state you are using a DataSet and TableAdapters. I am guessing that you are using a BindingSource to tie your Data to your DataGridView. If that is the case, then you can Filter your data via the BindingSource.
My set up is similar to this:
My combobox contains the fields that I want to use in my Filter and the textbox is the value that I will be applying. The values in the combobox are user-friendly names so they will understand which field they are filtering on.
The code to apply the filter is:
private void ApplyFilter()
{
var filterEntered = FilterTextBox.Text.Trim().ToLower();
MyBindingSource.RemoveFilter(); // remove previous filter
string filterText = string.Empty;
string filterComboText = string.Empty;
switch (FilterComboBox.Text)
{
case "Profile":
filterComboText = "TSProfile"; // column name in the query
break;
case "User Id":
filterComboText = "TSUserId";
break;
case "Center":
filterComboText = "TSCenter";
break;
case "Prefix":
filterComboText = "TSPrefix";
break;
}
filterComboText = filterComboText + " = '";
filterText += (string.IsNullOrEmpty(filterComboText) ? string.Empty : filterComboText);
filterText += (!string.IsNullOrEmpty(filterText) && !string.IsNullOrEmpty(filterEntered) ? filterEntered + "'" : string.Empty);
MyBindingSource.Filter = filterText;
}
Basically what it is doing, is getting the text name of the combo-box and then the text in the textbox and applying the Filter to the BindingSource.
MSDN has an article on Filtering thats contains full sample code.
The one thing that I recommend is to provide the user with a way to easily remove the filter, I use a Remove Filter button.
it would be helpful if you showed us a little code first..
you could try something like this tho:
in your codebehind, add items to your dropdownlist.
List<yourObject> list = new List<yourObject>();
foreach (yourObject i in list)
{
DropdownList1.Items.Add(new ListItem("" i.name, "" + i.id));
}
im just giving an example here, i.name could be the name of a certain student, i.id would be the id associated with that given student.
Make sure you have the autopostback attribute of your dropdownlist set to true, like this:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
Then in the selected Index Changed event of your dropdownlist, do the following:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
yourDataControl.DataSource = someMethod(Convert.toInt32(DropDownList1.SelectedValue));
yourDatacontrol.DataBind();
}
as i said, im not entirely sure what you're trying to do or how you're trying to do it.
the way i'm describing, you dont need the textbox to enter a certain value, by selecting an item in the dropdownlist you wil automatically get a value: in this case the ID associated with the selected item in the dropdownlist.

jquery append question

In my ASP.net project, I am using a jquery script to update the id=person field in the URL bar:
$("form").append("<input type='hidden' name='hiddenEmployeeId' value='" + $('#EmployeeSelected').val() + "' />");
This is triggered when the value of the drop-down box changes. It works great, except for the fact that when you change it...it just appends the new id behind the first id, which is exactly what it's supposed to be doing.
However, I only want one id, obviously. What can I do here to accomplish this? Is there a way to CLEAR that first, and then append it?
Thanks!
Check to see if it exists, if so, set the value. If it doesn't append it:
var input = $('input[name=hiddenEmployeeId]');
if(input [0]) {
input.val($('#EmployeeSelected').val());
} else {
$("form").append("<input type='hidden' name='hiddenEmployeeId' value='" + $('#EmployeeSelected').val() + "' />");
}
Cant you just change the value on the input named 'hiddenEmployeeId' or does it not exists the first time?
$("input[name=hiddenEmployeeId]").val($('#EmployeeSelected').val());
you can do something like this:
$($('input[name=hiddenEmployeeId]')[0] || $('<input type="hidden" name="hiddenEmployeeId">').appendTo("form.selector")[0]).val($('#EmployeeSelected').val());
and change the "form.selector" to the form you are working with.

Jquery and ASCX control rendered multiple times on a page

I have an ascx control which contains dropdownboxes I want to be able to reset with JavaScript.
Because the ascx control is rendered multiple times on the aspx page, I programatically add a distinguishing field to each dropdown such as this in the code behind of the ascx:
var g = Guid.NewGuid().ToString().Replace("-", "");
DropDownListBool.Attributes.Add("jqID", "ddBool" + g);
DropDownListEqual.Attributes.Add("jqID", "ddEq" + g);
On the rendered page, when I want to reset the dropdowns for one of the controls, I have a hyperlink which invokes a javascript function with g as an argument.
In the javascript, using jquery, I try to get both dropdowns for one specific ascx control like this:
function clearControl(g) {
var dds = $("select[jqID = 'dd\\S*" + g + "']");
}
I then do:
jQuery.each(dds, function(i, val) { val.select = 0; });
Should this work? Right now it is resetting seemingly random dropdown boxes.
Is there perhaps a limit to attribute length?
I think you might have better luck with a different selector, say the "ends with" attribute selector.
var dds = $("select[jqID$='" + g + "']");
If you needed to select based on starting with dd and ending with the value of g, you could use a filter and utilize both "ends with" and "starts with";
var dds = $("select[jqID^='dd']").filter( "[jqID$='" + g + '']");
As far as I know you can't use a regular expression when using the attribute equals selector. I'm surprised it works for you at all.

Categories

Resources