RadGrid Dynamic DataBound Columns - Check Each Cell Value - radgrid

I have a Radgrid who has Dynamic Databound fields which comes from a database query and each time they are different. Now when this data is displayed on the GRID, i want to change the value of Cells where its 0 to " " or "."
Thanks

You need to hook into the CellFormatting event. Here you can check the value of the cell and change it as required.
Something along the lines of:
Private Sub RadGrid_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles RadGrid.CellFormatting
if e.CellElement.RowInfo.Cells("ZeroColumn").Value = "0" then
e.CellElement.RowInfo.Cells("ZeroColumn").Value = "."
end if
End Sub

try
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 3; i < e.Row.Cells.Count; ++i)
{
TextBox tb = new TextBox();
tb.ID = "txtRow" + e.Row.RowIndex.ToString() + "Column" + i.ToString();
tb.Width = 50;
//if (Convert.ToBoolean(e.Row.Cells[i].Text) == false)
//tb.Text = "0";
//tb.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Controls.Add(tb);
// e.Row.Cells[i].Enabled = Convert.ToBoolean(e.Row.Cells[i].Text);
}
e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;
}
}
catch (Exception ex)
{ }

Related

How to change color of a part of text from < to > [duplicate]

I'm trying to color parts of a string to be appended to a RichTextBox. I have a string built from different strings.
string temp = "[" + DateTime.Now.ToShortTimeString() + "] " +
userid + " " + message + Environment.NewLine;
This is what the message would look like once it is constructed.
[9:23pm] User: my message here.
I want everything within and including the brackets [9:23] to be one color, 'user' to be another color and the message to be another color. Then I'd like the string appended to my RichTextBox.
How can I accomplish this?
Here is an extension method that overloads the AppendText method with a color parameter:
public static class RichTextBoxExtensions
{
public static void AppendText(this RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
}
And this is how you would use it:
var userid = "USER0001";
var message = "Access denied";
var box = new RichTextBox
{
Dock = DockStyle.Fill,
Font = new Font("Courier New", 10)
};
box.AppendText("[" + DateTime.Now.ToShortTimeString() + "]", Color.Red);
box.AppendText(" ");
box.AppendText(userid, Color.Green);
box.AppendText(": ");
box.AppendText(message, Color.Blue);
box.AppendText(Environment.NewLine);
new Form {Controls = {box}}.ShowDialog();
Note that you may notice some flickering if you're outputting a lot of messages. See this C# Corner article for ideas on how to reduce RichTextBox flicker.
I have expanded the method with font as a parameter:
public static void AppendText(this RichTextBox box, string text, Color color, Font font)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.SelectionFont = font;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
This is the modified version that I put in my code (I'm using .Net 4.5) but I think it should work on 4.0 too.
public void AppendText(string text, Color color, bool addNewLine = false)
{
box.SuspendLayout();
box.SelectionColor = color;
box.AppendText(addNewLine
? $"{text}{Environment.NewLine}"
: text);
box.ScrollToCaret();
box.ResumeLayout();
}
Differences with original one:
Possibility to add text to a new line or simply append it
No need to change selection, it works the same
Inserted ScrollToCaret to force autoscroll
Added SuspendLayout/ResumeLayout calls for better performance
EDIT : sorry this is a WPF answer
I think modifying a "selected text" in a RichTextBox isn't the right way to add colored text.
So here a method to add a "color block" :
Run run = new Run("This is my text");
run.Foreground = new SolidColorBrush(Colors.Red); // My Color
Paragraph paragraph = new Paragraph(run);
MyRichTextBlock.Document.Blocks.Add(paragraph);
From MSDN :
The Blocks property is the content property of RichTextBox. It is a
collection of Paragraph elements. Content in each Paragraph element
can contain the following elements:
Inline
InlineUIContainer
Run
Span
Bold
Hyperlink
Italic
Underline
LineBreak
So I think you have to split your string depending on parts color, and create as many Run objects as needed.
It`s work for me! I hope it will be useful to you!
public static RichTextBox RichTextBoxChangeWordColor(ref RichTextBox rtb, string startWord, string endWord, Color color)
{
rtb.SuspendLayout();
Point scroll = rtb.AutoScrollOffset;
int slct = rtb.SelectionIndent;
int ss = rtb.SelectionStart;
List<Point> ls = GetAllWordsIndecesBetween(rtb.Text, startWord, endWord, true);
foreach (var item in ls)
{
rtb.SelectionStart = item.X;
rtb.SelectionLength = item.Y - item.X;
rtb.SelectionColor = color;
}
rtb.SelectionStart = ss;
rtb.SelectionIndent = slct;
rtb.AutoScrollOffset = scroll;
rtb.ResumeLayout(true);
return rtb;
}
public static List<Point> GetAllWordsIndecesBetween(string intoText, string fromThis, string toThis,bool withSigns = true)
{
List<Point> result = new List<Point>();
Stack<int> stack = new Stack<int>();
bool start = false;
for (int i = 0; i < intoText.Length; i++)
{
string ssubstr = intoText.Substring(i);
if (ssubstr.StartsWith(fromThis) && ((fromThis == toThis && !start) || !ssubstr.StartsWith(toThis)))
{
if (!withSigns) i += fromThis.Length;
start = true;
stack.Push(i);
}
else if (ssubstr.StartsWith(toThis) )
{
if (withSigns) i += toThis.Length;
start = false;
if (stack.Count > 0)
{
int startindex = stack.Pop();
result.Add(new Point(startindex,i));
}
}
}
return result;
}
Selecting text as said from somebody, may the selection appear momentarily.
In Windows Forms applications there is no other solutions for the problem, but today I found a bad, working, way to solve: you can put a PictureBox in overlapping to the RichtextBox with the screenshot of if, during the selection and the changing color or font, making it after reappear all, when the operation is complete.
Code is here...
//The PictureBox has to be invisible before this, at creation
//tb variable is your RichTextBox
//inputPreview variable is your PictureBox
using (Graphics g = inputPreview.CreateGraphics())
{
Point loc = tb.PointToScreen(new Point(0, 0));
g.CopyFromScreen(loc, loc, tb.Size);
Point pt = tb.GetPositionFromCharIndex(tb.TextLength);
g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(pt.X, 0, 100, tb.Height));
}
inputPreview.Invalidate();
inputPreview.Show();
//Your code here (example: tb.Select(...); tb.SelectionColor = ...;)
inputPreview.Hide();
Better is to use WPF; this solution isn't perfect, but for Winform it works.
I created this Function after researching on the internet since I wanted to print an XML string when you select a row from a data grid view.
static void HighlightPhrase(RichTextBox box, string StartTag, string EndTag, string ControlTag, Color color1, Color color2)
{
int pos = box.SelectionStart;
string s = box.Text;
for (int ix = 0; ; )
{
int jx = s.IndexOf(StartTag, ix, StringComparison.CurrentCultureIgnoreCase);
if (jx < 0) break;
int ex = s.IndexOf(EndTag, ix, StringComparison.CurrentCultureIgnoreCase);
box.SelectionStart = jx;
box.SelectionLength = ex - jx + 1;
box.SelectionColor = color1;
int bx = s.IndexOf(ControlTag, ix, StringComparison.CurrentCultureIgnoreCase);
int bxtest = s.IndexOf(StartTag, (ex + 1), StringComparison.CurrentCultureIgnoreCase);
if (bx == bxtest)
{
box.SelectionStart = ex + 1;
box.SelectionLength = bx - ex + 1;
box.SelectionColor = color2;
}
ix = ex + 1;
}
box.SelectionStart = pos;
box.SelectionLength = 0;
}
and this is how you call it
HighlightPhrase(richTextBox1, "<", ">","</", Color.Red, Color.Black);
private void Log(string s , Color? c = null)
{
richTextBox.SelectionStart = richTextBox.TextLength;
richTextBox.SelectionLength = 0;
richTextBox.SelectionColor = c ?? Color.Black;
richTextBox.AppendText((richTextBox.Lines.Count() == 0 ? "" : Environment.NewLine) + DateTime.Now + "\t" + s);
richTextBox.SelectionColor = Color.Black;
}
Using Selection in WPF, aggregating from several other answers, no other code is required (except Severity enum and GetSeverityColor function)
public void Log(string msg, Severity severity = Severity.Info)
{
string ts = "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
string msg2 = ts + msg + "\n";
richTextBox.AppendText(msg2);
if (severity > Severity.Info)
{
int nlcount = msg2.ToCharArray().Count(a => a == '\n');
int len = msg2.Length + 3 * (nlcount)+2; //newlines are longer, this formula works fine
TextPointer myTextPointer1 = richTextBox.Document.ContentEnd.GetPositionAtOffset(-len);
TextPointer myTextPointer2 = richTextBox.Document.ContentEnd.GetPositionAtOffset(-1);
richTextBox.Selection.Select(myTextPointer1,myTextPointer2);
SolidColorBrush scb = new SolidColorBrush(GetSeverityColor(severity));
richTextBox.Selection.ApplyPropertyValue(TextElement.BackgroundProperty, scb);
}
richTextBox.ScrollToEnd();
}
I prepared a little helper for the RichTextBox control which makes it very easy to generate colored text on the screen:
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Common.Helpers
{
public class ColouredText
{
public string Text;
public Color Foreground;
public Color Background;
public ColouredText(string text, Color foreground, Color background)
{
Text = text;
Foreground = foreground;
Background = background;
}
public ColouredText(string text, Color foreground) : this(text, foreground, Color.Transparent) { }
public ColouredText(string text) : this(text, Color.Transparent, Color.Transparent) { }
}
public static class RichTextBoxHelper
{
private static RichTextBox _AppendText(RichTextBox box, string text, Color foreColor, Color backColor)
{
if (string.IsNullOrEmpty(text)) return box;
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = foreColor;
box.SelectionBackColor = backColor;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
return box;
}
private static void _UpdateText(RichTextBox box, IEnumerable<ColouredText> newTextWithColors)
{
box.Text = "";
foreach (var text in newTextWithColors)
{
var foreColor = text.Foreground; if (foreColor == Color.Transparent) foreColor = box.ForeColor;
var backColor = text.Background; if (backColor == Color.Transparent) backColor = box.BackColor;
_AppendText(box, text.Text, foreColor, backColor);
}
}
public static void UpdateText(this RichTextBox richTextbox, IEnumerable<ColouredText> text)
{
if (richTextbox.InvokeRequired) richTextbox.Invoke((MethodInvoker)(() => { _UpdateText(richTextbox, text); }));
else _UpdateText(richTextbox, text);
}
public static void UpdateText(this RichTextBox richTextbox, ColouredText text)
{
var list = new List<ColouredText>() { text };
if (richTextbox.InvokeRequired) richTextbox.Invoke((MethodInvoker)(() => { _UpdateText(richTextbox, list); }));
else _UpdateText(richTextbox, list);
}
}
}
and now you can use:
var text = new List<ColouredText>()
{
new ColouredText($"text#1 ", Color.Black),
new ColouredText($"text#2 ", Color.Red, Color.Yellow),
new ColouredText($" "),
new ColouredText($"text#2 ", Color.White, Color.Black)
};
richTextBox1.UpdateText(text);
or simpler usage for single-line text:
richTextBox1.UpdateText(new ColouredText($"warning message", Color.Yellow, Color.Red));

Recyclerview findViewHolderForAdapterPosition returns null after scroll to new position

RecyclerView recyclerView;
MyAdapter mAdapter;
List<ItemData> itemsData;
Global glb;
LinearLayoutManager llm;
private int valData() {
String tmpStr;
Cursor c;
int position=0;
enter code here
AutoCompleteTextView tTbl = (AutoCompleteTextView) findViewById(R.id.tblNam);
tmpStr = tTbl.getText().toString();
c = db.rawQuery("SELECT * FROM tblDts WHERE tblNam = '" + tmpStr + "'", null);
if (c.getCount() == 0) {
showMessage("Error", "Table Name Not Found");
c.close();
tTbl.requestFocus();
return 1;
}
c.close();
for (int i = 0; i < itemsData.size(); i++) {
tmpStr = itemsData.get(i).getTitle().toString();
c = db.rawQuery("SELECT * FROM itmDts WHERE itmNam = '" + tmpStr + "';", null);
if (c.getCount() == 0) {
c.close();
//llm.scrollToPositionWithOffset(i,0);
//recyclerView.smoothScrollToPosition(i);
//recyclerView.scrollToPosition(i);
i tried all the above scroll procedure, when i using getChildAt its return only view of the before scroll and not the new one
//View vw = llm.getChildAt(0);
tmpStr="";
when rise the findviewholderforadapterpostion in between current display views there has been it shows correctly but if i rise non display position it returns null
RecyclerView.ViewHolder vh = recyclerView.findViewHolderForAdapterPosition(i);
View vw = vh.itemView;
AutoCompleteTextView tTa=(AutoCompleteTextView)vw.findViewById(R.id.item_title);
tmpStr =tTa.getText().toString();
showMessage(llm.getChildCount()+"Error" + position, "Item Name Not Found" + tmpStr);
//recyclerView.setLayoutManager(lm);
return 1;
}
c.close();
}
return 0;
}
}
}
I am always facing with these kind of problems on Android platform. And most of my problems fixed with thread sleep :) This is not a proper way but could not found any other way. Just you need to add 100 ms sleep after scrolled the next item.
Following code is for Xamarin.Android.
public async static void FocusNextItem(Android.Support.V7.Widget.RecyclerView recyclerView, int nextPosition)
{
recyclerView.ScrollToPosition(nextPosition);
var viewHolder = recyclerView.FindViewHolderForAdapterPosition(nextPosition);
if (viewHolder == null)
{
await System.Threading.Tasks.Task.Delay(100);
viewHolder = recyclerView.FindViewHolderForAdapterPosition(nextPosition);
}
viewHolder?.ItemView?.RequestFocus();
}

Dynamically Create multiple combobox Codebehind & bind the value through wcf in silverlight

This is the code i have written to dynamically generate Controls:
private void GenCtrl(string type, int typeid,string sql)
{
string id = Convert.ToString(typeid);
if (type == "TextBox")
{// created for TextBox
}
else if (type == "ComboBox")
{
ComboBoxEdit cb = new ComboBoxEdit();
cb.Name = "cb" + id;
// this is to set unique id if more than 1 combobox created.
cb.Height = 20;
cb.Width = 100;
cb.Margin = new Thickness(2, 2, 0, 0);
cb.HorizontalAlignment = HorizontalAlignment.Left;
cb.Text = "Show All";
stctrl.Children.Add(cb);
ComboBoxEdit cbx = (ComboBoxEdit)cb.FindName(cb.Name);
cb.DisplayMember = "Name";
if(filsql !=null)
ServRef.GetComboBoxlistAsync(sql); //this would retrieve the list and attach to the combobox.
//ctlst.Add(cb.Name, type);
}
}
//WCF Service reference
ServRef.GetComboBoxlistCompleted += new EventHandler<GetComboBoxlistCompletedEventArgs>(ServRef_GetComboBoxlistCompleted);
void ServRef_GetComboBoxlistCompleted(object sender, GetComboBoxlistCompletedEventArgs e)
{
cb.ItemsSource = e.Result;
cb.SelectedIndex = 0;
}
The problem i am facing while binding two combobox is created ex : cb1,cb2( dynamically created)only the control cb2 combox which is the latest get activated at ServRef_GetComboBoxlistCompleted method. i am unable to bind the value for the first control(combobox).
if my question is not clear let me know.
i was able to find using FindName
ComboBoxEdit cbx = (ComboBoxEdit)stackctrl.FindName(cb.Name);

vb.net color first char in cell

In VB .NET I have 3 characters which are added to a DataGridView cell depending on some calculations.
They are rank change arrows and work fine, but I want the up arrow to be green and the down arrow to be red.
Dim strup As String = "▲"
Dim strdown As String = "▼"
Dim strsame As String = "▬"
So in the cell a change of negative three will look like ▼3 and plus 3 will look like ▲3 where the text and symbol are different colors.
How can I change the color of the first character in DataGridView cell?
There is no easy way to do this if you have anything more than just the character in question in the cell (you would need to do some form of custom painting).
If you only have those characters then this is very easy with the CellFormatting event:
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.CellStyle.Font = new Font("Arial Unicode MS", 12);
if (dataGridView1.Columns[e.ColumnIndex].Name == "CorrectColumnName")
{
if (e.Value == "▲")
e.CellStyle.ForeColor = Color.Green;
else if (e.Value == "▼")
e.CellStyle.ForeColor = Color.Red;
else
e.CellStyle.ForeColor = Color.Black;
}
}
If you do want different colors within the same cell then something like the following code is required (this handles the CellPainting event):
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 || e.RowIndex == -1)
return;
if (dataGridView1.Columns[e.ColumnIndex].Name == "CorrectColumnName")
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
if (e.FormattedValue.ToString().StartsWith("▲", StringComparison.InvariantCulture))
{
RenderCellText(Color.Green, e);
}
else if (e.FormattedValue == "▼")
{
RenderCellText(Color.Red, e);
}
else
RenderCellText(SystemColors.WindowText, e);
e.Handled = true;
}
}
private void RenderCellText(Color color, DataGridViewCellPaintingEventArgs e)
{
string text = e.FormattedValue.ToString();
string beginning = text.Substring(0, 1);
string end = text.Substring(1);
Point topLeft = new Point(e.CellBounds.X, e.CellBounds.Y + (e.CellBounds.Height / 4));
TextRenderer.DrawText(e.Graphics, beginning, this.dataGridView1.Font, topLeft, color);
Size s = TextRenderer.MeasureText(beginning, this.dataGridView1.Font);
Point p = new Point(topLeft.X + s.Width, topLeft.Y);
TextRenderer.DrawText(e.Graphics, end, this.dataGridView1.Font, p, SystemColors.WindowText);
}
I did something similar once and ended up putting these characters in their own column.

how to get the xtragrid filtered and sorted datasource?

I have an xtraGrid control (v12.1) binded to a bindingSource, this last gets its data from a LINQ to entities query (EF4.3.1), the end user can filter and sort the gridView, I have a Stimulsoft report that shows the content of the gridView when the user clicks on a PrintListButton, how to get the xtragrid filtered and sorted datasource, in order to attach it to the report?
Thanks.
var data = GetDataView(xtraGridControl1);
report.RegData("List", data.ToTable());
public DataView GetDataView(GridControl gc)
{
DataView dv = null;
if (gc.FocusedView != null && gc.FocusedView.DataSource != null)
{
var view = (ColumnView)gc.FocusedView;
var currentList = listBindingSource.List.CopyToDataTable().DefaultView; //(DataView)
var filterExpression = GetFilterExpression(view);
var sortExpression = GetSortExpression(view);
var currentFilter = currentList.RowFilter;
//create a new data view
dv = new DataView(currentList.Table) {Sort = sortExpression};
if (filterExpression != String.Empty)
{
if (currentFilter != String.Empty)
{
currentFilter += " AND ";
}
currentFilter += filterExpression;
}
dv.RowFilter = currentFilter;
}
return dv;
}
public string GetFilterExpression(ColumnView view)
{
var expression = String.Empty;
if (view.ActiveFilter != null && view.ActiveFilterEnabled
&& view.ActiveFilter.Expression != String.Empty)
{
expression = view.ActiveFilter.Expression;
}
return expression;
}
public string GetSortExpression(ColumnView view)
{
var expression = String.Empty;
foreach (GridColumnSortInfo info in view.SortInfo)
{
expression += string.Format("[{0}]", info.Column.FieldName);
if (info.SortOrder == DevExpress.Data.ColumnSortOrder.Descending)
expression += " DESC";
else
expression += " ASC";
expression += ", ";
}
return expression.TrimEnd(',', ' ');
}