Truncate text in AspxGridView cells - devexpress

Is there a way to truncate long text in AspxGridView cells?
I've read and implemented this solution. , http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_CustomColumnDisplayTexttopic
...which of course works BUT only for one column and I need to create this with several columns.
Here is my solution so far
protected void AsPxGridView1CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName != "AnalysisFeedbackAuto") return;
if (e.Value.ToString().Length > 13)
{
var displayText = Regex.Replace(e.Value.ToString(), "<.*?>", string.Empty).Substring(0, 10);
e.DisplayText = string.Concat(displayText, "...");
}
}
Any advice?
Thanks
=== UPDATE ===
Obviously this was the solution
protected void AsPxGridView1CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "AnalysisFeedbackAuto"
|| e.Column.FieldName == "AnalysisResults"
|| e.Column.FieldName == "AnalysisAnswers"
)
{
if (e.Value.ToString().Length > 13)
{
var displayText = Regex.Replace(e.Value.ToString(), "<.*?>", string.Empty).Substring(0, 10);
e.DisplayText = string.Concat(displayText, "...");
}
}
}

Here are solutions from DevExpress:
http://www.devexpress.com/issue=Q300507
http://www.devexpress.com/issue=Q303093

protected void AsPxGridView1CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "AnalysisFeedbackAuto"
|| e.Column.FieldName == "AnalysisResults"
|| e.Column.FieldName == "AnalysisAnswers"
)
{
if (e.Value.ToString().Length > 13)
{
var displayText = Regex.Replace(e.Value.ToString(), "<.*?>", string.Empty).Substring(0, 10);
e.DisplayText = string.Concat(displayText, "...");
}
}
}

Related

How to paint cells in row (Telerik)?

I've next code that handle fowFormatting my cells:
private void gridViewRaces_RowFormatting(object sender, RowFormattingEventArgs e)
{
foreach (var cellColumn in e.RowElement.Data.Cells)
{
var cellInfo = cellColumn as GridViewCellInfo;
if (cellInfo != null)
{
cellInfo.Style.DrawFill = true;
if (cellInfo.ColumnInfo.Name == "columnContactProducerName")
{
cellInfo.Style.DrawFill = true;
cellInfo.Style.BackColor = Color.Yellow;
}
else if (cellInfo.ColumnInfo.Name == "columnTransport")
{
cellInfo.Style.BackColor = Color.Yellow;
}
else
{
cellInfo.Style.BackColor = ColorTranslator.FromHtml((e.RowElement.Data.DataBoundItem as RaceForListDto).Color);
}
}
}
//e.RowElement.BackColor = ColorTranslator.FromHtml((e.RowElement.Data.DataBoundItem as RaceForListDto).Color);
}
but my cells aren't painting. How to paint some cells in rows on dataBinding?
It looks like the proper event to do this is ItemDataBound event. See here:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/appearance-and-styling/conditional-formatting
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
//Is it a GridDataItem
if (e.Item is GridDataItem)
{
//Get the instance of the right type
GridDataItem dataBoundItem = e.Item as GridDataItem;
//Check the formatting condition
if (int.Parse(dataBoundItem["Size"].Text) > 100)
{
dataBoundItem["Received"].ForeColor = Color.Red;
dataBoundItem["Received"].Font.Bold = true;
//Customize more...
}
}
}
Or event better is to use a custom CSS class so that you can later make changes without having to rebuild the project:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
if (dataItem["Country"].Text == "Mexico")
dataItem.CssClass = "MyMexicoRowClass";
}
}

Force String.Format "{0:P4}" to show + sign

I have a decimal column in my Database where values are stored as 12.35
We show it as 12.35%
The client wants to show +12.35% if the value is positive(just for this one field). How I do get it to show the +sign.
We format the textedit as P4 in the getter String.Format("{0:P4}", value);
This is what I've tried:
I was able to do this by using Fomrat event handler. I am looking for a cleaner way instead of the below code.
private void txtMargin_FormatEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
{
if (e.Value != null)
{
if (e.Value.ToString().IndexOfAny(new char[] { '-', '+' }) < 0)
{
string val = e.Value.ToString();
val = val.Replace("%", "");
e.Value = string.Format("+{0}", (Convert.ToDouble(val) / 100).ToString("P4"));
e.Handled = true;
}
else
{
string val = e.Value.ToString();
val = val.Replace("%", "");
e.Value = (Convert.ToDouble(val) / 100).ToString("P4");
}
e.Handled = true;
}
}
private void txtMargin_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
{
if (e.Value != null)
{
if (e.Value.ToString().IndexOf('%') < 0)
{
e.Value = (Convert.ToDouble(e.Value.ToString()) / 100).ToString("P4");
}
}
}
In your form load past this code :
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textEdit1.Properties.Mask.EditMask = "+#0.0000% ;-#0.0000%";
textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
textEdit1.Properties.Mask.UseMaskAsDisplayFormat = false;
textEdit1.Properties.EditFormat.FormatString = "+#0.0000% ;-#0.0000%";;
}
And in you TextBox Handel the event "`CustomDisplayText`" as :
private void textEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
if (e.Value != null && !e.Value.Equals (""))
e.DisplayText = (Convert.ToDouble(e.Value.ToString()) / 100).ToString("+#0.0000 % ;-#0.0000 %");
}

Custom ASPxGridViewExporter

I have an aspxgridview, I change displayed text via "CustomColumnDisplayText" event, My problem is when I want to use ASPxGridViewExporter for excel output, one of columns shows wrong data. I don't know how to use ASPxGridViewExporter RenderBrick event.
protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.PropertiesEdit.DisplayFormatString.Equals("Status"))
{
if (Convert.ToInt64(e.Value) == 2)
e.Value = "Successful";
else if (e.GetFieldValue("SaleReferenceId") == null || e.GetFieldValue("SaleReferenceId").ToString() == string.Empty || e.GetFieldValue("SaleReferenceId").ToString().Trim().Equals(""))
e.Value = "Invalid";
else if (e.GetFieldValue("saleOrderId") == null || e.GetFieldValue("saleOrderId").ToString() == string.Empty || e.GetFieldValue("saleOrderId").ToString().Trim().Equals(""))
e.Value = "Invalid";
else
e.Value = "Unsuccessful";
e.DisplayText = e.Value.ToString();
}
}
protected void ASPxGridViewExporter1_RenderBrick(object sender, DevExpress.Web.ASPxGridView.Export.ASPxGridViewExportRenderingEventArgs e)
{
// I don't know how to use it
}
I don't know how but this snippet works fine for me and the result is exactly what I wanted!
protected void ASPxGridViewExporter1_RenderBrick(object sender, DevExpress.Web.ASPxGridView.Export.ASPxGridViewExportRenderingEventArgs e)
{
try
{
GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
if (dataColumn.FieldName == "Status")
{
e.TextValue = "123";
}
}
catch (Exception ex)
{
}
}

ASP.NET Session value not changing - TicTacToe app

So, I'm trying to make a TicTacToe application, but the problem is the clickNo counter which should be counting clicks (after every click) is not being updated after the click. I've tried using if IsPostBack == false in Page Load, as someone suggested but the only thing that it accomplished is that a "O" randomly pops out.
{
public partial class home : System.Web.UI.Page
{
public int clickedByPlayerNo, clickNo;
protected void Page_Load(object sender, EventArgs e)
{
Session["s1"] = clickNo;
}
public void buttonClicked(object Sender)
{
clickNo = (int)Session["s1"];
if (Session["s1"] != null)
{
clickNo = +1;
if (clickNo % 2 == 0)
{
clickedByPlayerNo = 2;
}
else
{
clickedByPlayerNo = 1;
}
if (clickedByPlayerNo == 1)
{
((Button)Sender).Text = "X";
}
else
{
((Button)Sender).Text = "O";
}
((Button)Sender).Enabled = false;
Session["s1"] = clickNo;
checkWinner();
}
else
{
Response.Write("Session expire");
}
}
public void checkWinner()
{
if (clickNo > 4)
{
if (btn1.Text == btn2.Text && btn2.Text == btn3.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn4.Text == btn5.Text && btn5.Text == btn6.Text && btn4.Text != "")
{
displayWinner();
}
else if (btn7.Text == btn8.Text && btn8.Text == btn9.Text && btn7.Text != "")
{
displayWinner();
}
else if (btn1.Text == btn4.Text && btn4.Text == btn7.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn2.Text == btn5.Text && btn5.Text == btn8.Text && btn2.Text != "")
{
displayWinner();
}
else if (btn3.Text == btn6.Text && btn6.Text == btn9.Text && btn3.Text != "")
{
displayWinner();
}
else if (btn1.Text == btn5.Text && btn5.Text == btn9.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn3.Text == btn5.Text && btn5.Text == btn7.Text && btn3.Text != "")
{
displayWinner();
}
else if (clickNo == 9)
{
Response.Write("Game Draw");
clearWindow();
}
else
{
Response.Write("Session expire");
}
}
}
public void displayWinner()
{
if (clickedByPlayerNo == 1)
{
Response.Write("Winner is A");
}
else
Response.Write("Winner is B");
clearWindow();
}
public void clearWindow()
{
btn1.Text = "";
btn1.Enabled = false;
btn2.Text = "";
btn2.Enabled = false;
btn3.Text = "";
btn3.Enabled = false;
btn4.Text = "";
btn4.Enabled = false;
btn5.Text = "";
btn5.Enabled = false;
btn6.Text = "";
btn6.Enabled = false;
btn7.Text = "";
btn7.Enabled = false;
btn8.Text = "";
btn8.Enabled = false;
btn9.Text = "";
btn9.Enabled = false;
}
protected void btn1_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn2_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn3_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn4_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn5_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn6_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn7_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn8_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn9_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
}
}
My sincerest thanks to Red, I've been searching the net and looking at the monitor for 10 hours straight. Here's the updated part of the code:
public partial class home : System.Web.UI.Page
{
public int clickedByPlayerNo, clickNo;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s1"] = "-1";
}
}
public void buttonClicked(object Sender)
{
clickNo = Convert.ToInt32(Session["s1"]);
if (Session["s1"] != null)
{
int someValue = clickNo + 1;
if (clickNo % 2 == 0)
{
clickedByPlayerNo = 2;
}
else
{
clickedByPlayerNo = 1;
}
if (clickedByPlayerNo == 1)
{
((Button)Sender).Text = "X";
}
else
{
((Button)Sender).Text = "O";
}
((Button)Sender).Enabled = false;
Session["s1"] = someValue;
}
checkWinner();
}
Take a closer look at Page_Load method. Every time you have a postback posted by one of your buttons, Page_Load method is called. What you need to do is to fill the session variable only once at page load. Here is what I would do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s1"] = "0";
}
}

error "A data item was not found in the container" in the nested grid view

In the main gridview "GridViewTtransmittals" I have the nested gridview with the name "GridViewTranstoCon".When "GridViewTranstoCon" has data, I have no problem, but when there is not any data for that it returns error "A data item was not found in the container", I like it to return null value or have it invisible.
The code is like below:
protected void GridViewTtransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == null)
return;
TransmittaltoConPresentationModel transmittaltoCon = (TransmittaltoConPresentationModel)e.Row.DataItem;
GridView g2 = (GridView)e.Row.FindControl("GridViewTranstoCon");
if (transmittaltoCon.TranstoCons != null)
{
g2.DataSource = transmittaltoCon.TranstoCons;
g2.DataBind();
}
}
The code for data source of main gridview "GridViewTtransmittals" comes of below code:
private void DisplayTransmittals()
{
if (_Transmittals.Any())
{
var query = from transmittalno in _Transmittals
select new TransmittaltoConPresentationModel { TransID = transmittalno.TransID,
Transmittal = transmittalno.TRANSMITTAL, TranstoCons = from doctranstocon in _DocTranstoCons
where doctranstocon == null || transmittalno.TransID == doctranstocon.Transid
select doctranstocon != null ? doctranstocon.tblTranstoCon : null};
GridViewTtransmittals.DataSource = query;
}
else
{
GridViewTtransmittals.DataSource = null;
}
GridViewTtransmittals.DataBind();
}
Try to include this
protected void GridViewTtransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
}
}

Resources