this question might be very basic. But I just want to make this clear:
lets say I have a code like :
string currentToner = (string)((Hashtable)ht[1])["value"];
string maxToner = (string)((Hashtable)ht[2])["value"];
Now I want to set this value in data grid view.
int number = dataGridView1.Rows.Add();
dataGridView1.Rows[number].Cells[0].Value = currentToner."/".maxToner; //-->this is not the correct way.
How to set the value so that it looks something like:
5000/10000
You are doing it the PHP way, in C# string concatenation is done with plus signs
dataGridView1.Rows[number].Cells[0].Value = currentToner + "/" + maxToner;
You can format the string as well
dataGridView1.Rows[number].Cells[0].Value = string.Format("{0}/{1}", currentToner, maxToner);
If you want to display a string, set a string as the value :
dataGridView1.Rows[number].Cells[0].Value = currentToner + "/" + maxToner;
Related
i would like to filter a datatable by a column which contains number data, i'm trying the following:
string selected = colSelect.GetItemText(colSelect.SelectedItem);
if (filterText.Text.Length == 0)
{
data_table.DefaultView.RowFilter = string.Empty;
}
else
{
data_table.DefaultView.RowFilter = string.Format("Price Like '%{0}%'", filterText.Text);
I've tried casting the second value to a string but no luck, i get the error:
Cannot perform 'Like' operation on System.Decimal and System.String
The data entered would be any number or text, but based on the data only relevant number values would show with the filter.
Looks like Price column is decimal typed but you supplied a string which is filterText.Text to filter it.
One solution might be using CONVERT which is explained in DataColumn.Expression documentation like;
data_table.DefaultView.RowFilter = "CONVERT(Price, 'System.String') like '%" + filterText.Text + "%'";
I didn't tried this one but you can parse your filterText.Text to decimal (if it is valid one) and use it like;
data_table.DefaultView.RowFilter = "Price Like '%" + decimal.Parse(filterText.Text) + "%'";
But as I said, I an not %100 sure for the second one.
This might be a problem with Session and not ToString(), I'm not sure.
I have two .aspx pages and I want to pass an IP address from a datatable from one page to the other. When I do this, spaces get added that I don't want. The simple version of the code is this:
first .aspx page
int num = DropDownList1.SelectedIndex;
DataView tempDV = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2];
Response.Redirect("test.aspx");
test.aspx page
string ipCamAdd = Session["camera"].ToString();
TextBox1.Text = "http://" + ipCamAdd + "/jpg/image.jpg?resolution=320x240";
what I want to print is
http ://ipadd/jpg/image.jpg?resolution=320x240
but what prints out is
http//ipaddress /jpg/image.jpg?resolution=320x240
how can I fix this?
Also, I asked this question hoping someone could tell me why this is happening as well. Sorry for the mistake.
Try this:
string ipCamAdd = Session["camera"].Trim().ToString();
For the valid concern, Session["camera"] could be null, add function such as the following to your code
static string ToSafeString(string theVal)
{
string theAns;
theAns = (theVal==null ? "" : theVal);
return theAns;
}
Then use:
string ipCamAdd = Session["camera"].ToSafeString().Trim();
You can use string.Replace if you just want to get rid of the spaces:
TextBox1.Text = "http://" + (ipCamAdd ?? "").Replace(" ", "") + "/jpg/image.jpg?resolution=320x240";
Trim the result before setting to session.
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2].Trim();
Seems In SQL your data type is char(*) if you convert the data type to varchar and re enter data, you wont get any additional spaces
I have a DataGridView in which I have multiple rows say (15 rows). I want to add some columns value and store them in an array using foreach loop. Then I want to display the array (with multiple strings) in another form.
Currently I am using stringBuilder to store the strings and display, but it only display the last string store in it.
StringBuilder listOrderStatus = new StringBuilder();
foreach (DataGridViewRow rw in grdData.Rows)
{
string _idNumber = rw.Cells[6].Value.ToString();
string _orderNo = rw.Cells[13].Value.ToString();
double _ordTotalSpace = double.Parse(rw.Cells[17].Value.ToString());
double _ordDoneSpace = double.Parse(rw.Cells[18].Value.ToString());
double _ordRmainSpace = double.Parse(rw.Cells[19].Value.ToString());
double _TotalSpace = getTotalArea();
double _DoneSpace = getDoneArea();
double _RmainSpace = getRmainArea();
if(_ordTotalSpace ! = _TotalSpace){
string value = "Invalid Order: Order number" + _orderNo + "ID No." + _idNumber;
//I want to store this string in the array
listOrderStatus.Append(value);
listOrderStatus.AppendLine();
}
else{
string value2 = "Valid Order: Order number" + _orderNo + "ID No." + _idNumber;
//I want to store this string in the array
listOrderStatus.Append(value2);
listOrderStatus.AppendLine();
}
}
string innerString = listOrderStatus.ToString();
//This a new form to display strings in simple multiline-TextBox (Never mind, this is client request :P)
var myForm = new Other.frmDisplayData();
myForm._clist = innerString;
myForm.Show();
Can any one guide me. Thanks in advance
Are you insisting on using StringBuilder?! if not you can use something like below:
Define as global and instead of StringBuilder:
List<string> listOrderStatus = new List<string>();
And then
string value = "Invalid Order: Order number" + _orderNo + "ID No." + _idNumber;
listOrderStatus.Add(value);
then
myForm._clist = listOrderStatus;
Note:
You have to define _clist as a List of strings like first code line I wrote.
in this case it's not needed to use new line .but if you will want to use do like below:
string value = "Invalid Order: Order number" + _orderNo + "ID No." + _idNumber;
value += System.Environment.NewLine;
you can also use Dictionary definition :
Dictionary<int,string> Name = new Dictionary<int,string>();
How is _clist used to assign to multiLineTextBox.Text?
multilineTextBox.Multiline = true;
multilineTextBox.Scrollbars = Scrollbars.Both;
multilineTextBox.Text = _clist;
This would at least be the minimum of what you would need? Are you able to see in Debug that the _clist has the entire string that you're wanting to display?
i have string in textbox:
`New-Value = 12,34 -- Old-Values: 12,31,`
what i'd like to do is to get Old-Value so "12,31,"
How can i get from this textbox this specific information do this? So value is between ":" and ","
Tnx
Regex.Match("New-Value = 12,34 -- Old-Values: 12,31,",#"\:(.+)\,").Groups[1].Value.Trim()
const string oldPointer = "Old-Values: ";
var text = "New-Value = 12,34 -- Old-Values: 12,31,";
var old = text.Substring(text.IndexOf(oldPointer) + oldPointer.Length).TrimEnd(',');
Not very clear if this is a fixed (static) format of your string, but by the way:
A simple solution could be:
string str = "New-Value = 12,34 -- Old-Values: 12,31,";
str.Substring(str.IndexOf(':') + 1);
More complex one should involve Regular expressions, like an answer of L.B or others if any.
The below string is coming from a DIV tag. So I have enclosed the value below.
String cLocation = "'target="_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'"
I would like to replace in the above string by changing "src="/" with "src='xyz/files'".
I have tried the typical string.Replace("old","new") but it didn't work.
I tried the below,
cNewLocation ="xyz/files";
cNewString = cLocation.Replce("src='/'", "src='" + cNewLocation + "'/")
It didn't work.
Please suggest.
If I'm understanding what you're asking, you could use Regex to replace the string like so:
var cNewString = Regex.Replace(cLocation, #"src='/.*/", "src='" + newLocation + "/");
EDIT : I modified the regular expression to replace src='/.../ with src='{newLocation}/
you might try looking at the Replace command in c#.
so mystring = srcstring.Replace("old", "New");
http://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.71%29.aspx
possible replace the / in the string with //?
You can do the following:
string cLocation = "'target='_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'";
cLocation = cLocation.Replace("src='/'", "src='xyz/files'");
This fixes the problem:
int start = cLocation.IndexOf("src='") + 5;
int end = cLocation.LastIndexOf("'");
string xcLocation = cLocation.Remove(start, end - start);
string cLocation = xcLocation.Insert(start , "xyz/files");