How to format column to show percent (%) in the xtragrid - devexpress

I have a xtragrid with values from a stored procedure.
I am getting the values in float (0.23) and I want to display in percent (23%).
What would be the best way to do it in C#?
before
after

Use CustomDrawCell event if you just want to display the cell readonly. or You can use the CustomColumnDisplayText Event also.
Try this:
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.FieldName == "Marks")
{
e.DisplayText = (((int)(Convert.ToDecimal(e.CellValue) * 100))).ToString() + "%";
}
}
Another way use the Editor EditFormat and DisplayFormat property. and after setting these properties add it to the gridview column: Ref: How to format values shown in the XtraGrid
RepositoryItem textEdit;
private void AddGridRepositoryItem()
{
textEdit = new RepositoryItemTextEdit();
textEdit.AllowHtmlDraw = DevExpress.Utils.DefaultBoolean.True;
textEdit.ReadOnly = true;
gridControl1.RepositoryItems.Add(textEdit);
textEdit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
textEdit.DisplayFormat.FormatString = "p";
gridView1.Columns[0].ColumnEdit = textEdit;
}
The simplest way is use GridColumn.DisplayFormat Property.
colPayment.DisplayFormat.FormatType = FormatType.Numeric;
colPayment.DisplayFormat.FormatString = "p0";
Hope this help..

Please use the following approach:
colReabilitation.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
colReabilitation.DisplayFormat.FormatString = "p0";
Related links:
GridColumn.DisplayFormat
Formatting Values
Standard Numeric Format Strings(MSDN)

Related

Export ASPxGridView with column with multiline text to excel

Is it possible to export ASPxGridView with column which contains multiline text to excel using ASPxGridViewExporter?
I have an ASPxGridView with column that contains a multiline text.
When I export a grid's data using ASPxGridViewExporter that multiline text is rendered as one line (without line breaks).
I tried both <br/> and "\n" (newline) as line separators.
btw value of property PropertiesTextEdit-EncodeHtml is false on that column.
Thanks
There are 2 ways to achieve what you're looking for:
Specify WYSIWYG export type in XlsxExportOptionsEx:
XlsxExportOptionsEx options = new XlsxExportOptionsEx()
{
ExportType = DevExpress.Export.ExportType.WYSIWYG
};
ASPxGridView1.ExportToXlsx("Test.xlsx", options);
Tell the exporter you want to have data aware export and handle CustomizeCell event to set cell wrapping to true:
XlsxExportOptionsEx options = new XlsxExportOptionsEx()
{
ExportType = DevExpress.Export.ExportType.DataAware
};
options.CustomizeCell += options_CustomizeCell;
void options_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs e)
{
e.Formatting.Alignment = new XlCellAlignment() { WrapText = true };
e.Handled = true;
}
Then use the customized options object for export.
See: https://www.devexpress.com/Support/Center/Question/Details/T381176
There is also the RenderBrick event which sometimes may be helpful. You may handle it like:
gveExporter.RenderBrick += gveExporter_RenderBrick;
void gveExporter_RenderBrick(object sender, DevExpress.Web.ASPxGridViewExportRenderingEventArgs e)
{
...
StringFormat sFormat = new StringFormat(StringFormatFlags.NoWrap);
BrickStringFormat brickSFormat = new BrickStringFormat(sFormat);
e.BrickStyle.StringFormat = brickSFormat;
...
}
But I have not found how to actually force cell wrap there, because StringFormatFlags has only NoWrap among suitable items. In my experience I did have cell wrapping long text in the exported Excel doc, so I've used RenderBrick to switch that wrapping off.
Hope that helps.

Depending source for drop down list

I have one drop down list in my pages that its source comes of below code. Now I like to put 1 text box adjusted on my drop down list and when I type on that, source of drop down list (DocumentNo) depend on what I type in the text box and when text box is null drop downs list shows all the (DocumentNo) , please help how I have to change my code,
protected void ddlProjectDocument_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var query = from p in _DataContext.tblDocuments
orderby p.DocumentNo
select p;
int maxs = 0;
foreach (tblDocument v in query)
{
if (v.DocumentNo.Length > maxs)
maxs = v.DocumentNo.Length;
}
foreach (tblDocument vv in query)
{
string doctitle = vv.DocumentNo;
for (int i = vv.DocumentNo.Length; i < maxs; i++)
{
doctitle += " ";
}
doctitle += " | ";
doctitle += vv.TITLE;
// Use HtmlDecode to correctly show the spaces
doctitle = HttpUtility.HtmlDecode(doctitle);
ddlProjectDocument.Items.Add(new ListItem(doctitle, vv.DocId.ToString()));
}
}
First, I would highly recommend storing the result of that query at the beginning of the method into something like a session variable so that you don't have to continually query the database every time you hit this page.
Second, you should use the OnTextChanged event in ASP.NET to solve this problem. Put in the OnTextChanged attribute to point to a method in your code behind that will grab the query result values (now found in your session variable) and will reset what is contained in ddlProjectDocument.Items to anything that matched what was being written by using String.StartsWith():
var newListOfThings = queryResults.Where(q => q.DocumentNo.StartsWith(MyTextBox.Value));
At this point all you need to do is do that same loop that you did at the end of the method above to introduce the correct formatting.

get value from Linq and put it in the tex box

i have this code
MasterSoapClient sp = new MasterSoapClient();
MasterData[] lstMasterData = sp.GetActivityType(stid, null, 1);
grdEditActivityType.DataSource = lstMasterData;
grdEditActivityType.DataBind();
Session["opType"] = 2;
txtActivityCode.Text = lstMasterData.ToString();
txtActivityCode.DataBind();
here i called web service and put all data in this Gridview "grdEditActivityType "
and already workin
but there is column of lstMasterData i want to put it in the text box out of the grid
how i can do this ?
txtActivityCode.Text is a Property that you can use to assign a text value to the TextBox.
If you use txtActivity.Text = " some input "; Your textbox will contain the text " some input ".
You don't need to Bind txtActivityCode afterwards.
Having this clarified the next step is to create the string you want using to assign to the text box.
string s = "";
foreach( var masterData in lstMasterData )
{
s += masterData.SomeProperty; // s += masterData.ToString(); maybe, it depends on what do you want to put in the textbox;
}
txtActivityCode.Text = s;
And that's all.
I would suggest to start look more over ASP.NET tutorials to understand better how this framework works.

Insert a control before another control inside a ItemDataBound event

I tried to Solution suggested here, but it didn't work in my case. using Page.Controls.IndexOf() for any of the elements on my page, when called in the ItemDataBound event method, returns -1.
I need to insert a linebreak based on certain conditions for stuff generated by my Data repeater. Here is the method:
private String lastCharacter = "";
public void users_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
HyperLink link = (HyperLink)e.Item.FindControl("micrositeLink");
Tuple<String, String> user = (Tuple<String, String>)e.Item.DataItem;
link.NavigateUrl = "/" + user.Item1;
link.Text = user.Item2;
// makes a break in the data when going from one bunch of data to another.
if (user.Item1.Length >= 2)
{
if (lastCharacter == "")
lastCharacter = user.Item1[1].ToString().ToLower();
else if (lastCharacter != user.Item1[1].ToString().ToLower())
{
HtmlGenericControl lineBreak = new HtmlGenericControl("br");
if (Page.Controls.IndexOf(link) >= 0)
Page.Controls.AddAt(Page.Controls.IndexOf(link), lineBreak);
lastCharacter = user.Item1[1].ToString().ToLower();
}
}
}
The bound data is a list of users in my system with names beginning with a particular letter. My goal is to further sub-divide this data with a line break between groups of data that have the same second letter. For instance:
AaPerson Aarad AaStuff
Aathing
AbItem AbStuff
Acan Achandle
To me, inserting a line break before the elements where the second letter changes is the obvious solution, but other suggestions are also appreciated.
Try using e.Item.Controls.IndexOf instead:
if (e.Item.Controls.IndexOf(link) >= 0)
e.Item.Controls.AddAt(e.Item.Controls.IndexOf(link), lineBreak);

How to show pop up menu from database in gridview on each gridview row items?

How to show pop up menu from database in gridview on each gridview row items ?
Example of this is :
http://www.redbus.in/Booking/SelectBus.aspx?fromCityId=733&fromCityName=Delhi&toCityId=757&toCityName=Manali&doj=26-Dec-2010&busType=Any
Move your cursor to Departure time and arrival time...a want this type of popup in gridview items....which fetch entries from database..
You can handle this with javascript/jQuery. The gridview itself has nothing to do with this.
If I was going to build what he has just by looking at it I would create the table dynamically. This way you could use a string builder and insert variable names in between each td tag. You retrieve all your data from the database and store in a List or even better use LINQ to SQL.
I wish I could give you a sample in VB, but here is an example of what I'm talking about.
protected void Page_Load(object sender, EventArgs e)
{
StoreDataContext db = new StoreDataContext();
var join = from b in db.Brands
select new
{
Brand = b,
c = b.Description.Length < 204 ? b.Description : b.Description.Substring(0, 204) + "...",
Sources =
from s in db.Sources
join xref in db.Brands_Sources on s.SourceID equals xref.SourceID
where xref.BrandID == b.BrandID
select s
};
StringBuilder sb = new StringBuilder();
sb.Append( "<table class='tableStripe'>");
sb.Append( "<tr><th width='1%'>Active</th><th>Image</th><th>Name</th><th>Short Description</th><th>Source</th><th width='1%'>Date Created</th><th width='1%'>Data Modified</th></tr>");
foreach (var result in join)
{
string chk = (result.Brand.Active ? "checked='checked'" : "");
sb.AppendFormat( "<tr><td><input class='brandChk' value='{4}' type='checkbox' {0}></td><td width='1%'><img width='50px' src='{1}'</img></td>" +
"<td><a href='/admin/Catalog/Brand/Detail.aspx?brandId={4}'>{2}</a></td><td width='60%'>{3}</td>", chk, result.Brand.Image, result.Brand.Name, result.c,result.Brand.BrandID);
sb.Append("<td>");
foreach (var q in result.Sources)
{
string srcname = (q.Source1=="Motovicity" ? "Motovicity":"Direct");
sb.AppendFormat("<img src='{0}' title='{1}'</img>", q.Image,srcname);
}
string date = string.Format("{0:MM/dd/yy}", result.Brand.DateCreated);
string mod = string.Format("{0:MM/dd/yy}", result.Brand.DateModified);
sb.Append("</td>");
sb.AppendFormat("<td>{0}</td><td>{1}</td></tr>", date, mod);
}
sb.Append("</table>");
resultSpan.InnerHtml = sb.ToString();
}
As you can see I code the html for a checkbox and insert the brand id into the value attribute.
He is not fetching record on runtime basically if you see the structure he has the information in the tooltip attribute of anchor tag and using jquery cluetip to display it.
so you can use any jquery tooltip plugin to display same as he is doing.
Thanks

Resources