Change GridRowStyle in Telerik WinForm - winforms

i have a field in my database for detect font syle of a row.
font syle is Regular where it is true.
I want to changing my row style when select it. i write this :
private void myGrid_SelectionChanged(object sender, EventArgs e)
{
DataBaseComponent.EditFieldofObject(object1.Serial, true);
if (myGrid.SelectedRows[0].VisualElement != null)
myGrid.SelectedRows[0].VisualElement.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
myGrid.SelectedRows[0].Cells["myField"].Value = true;
}
but it doesnot work and i must bind grid again to see this change.

Why not use ItemDataBound instead of SelectionChanged? This will work for your needs.
protected void myGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataBoundItem = (GridDataItem)e.Item;
if (dataBoundItem["ColumnName"].Text.ToString() == "True")
{
// Do something here
}
}
}
There is a good article on Telerik explaining it.

Related

How can I validate only the controls inside a panel?

I have a form with two panels that each have a [Save] button.
How can I validate all of the controls inside each panel separately?
I was hoping that the Panel class would have a Validate() method but it doesn't. It's also not a ContainerControl so it also doesn't have a ValidateChildren method.
What's the best way to accomplish this?
If you set your Form's AutoValidate mode to EnableAllowFocusChange, and presuming you have validating events hooked up to each of the controls inside your panel, something like this:
private void tb_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
if (tb.Text == String.Empty)
{
errorProvider1.SetError(tb, "Textbox cannot be empty");
e.Cancel = true;
}
else
errorProvider1.SetError(tb, "");
}
}
Then on the Click handler for your save button, you can just do this:
private void SaveButton_Click(object sender, EventArgs e)
{
foreach (Control c in panel1.Controls)
c.Focus();
// If you want to summarise the errors
StringBuilder errorSummary = new StringBuilder();
foreach (Control c in panel1.Controls){
String error = errorProvider1.GetError(c);
if (error != String.Empty)
errorSummary.AppendFormat("{0}{1}", errorProvider1.GetError(c), Environment.NewLine);
}
if(errorSummary.Length>0)
MessageBox.Show(errorSummary.ToString());
}
That will cause the validation to fire on each of the controls within the panel.

Get RowIndex via ContextMenu?

I'm trying to get a rowindex of row at which I right clicked to call a contextmenu.
DatagridView's property contextmenu is set to this contextmenu.
Is it possible in some simple way?
Best regards
Yes, you need to handle the MouseDown event for your DataGridView and then use the HitTest method to return row and/or column index for the given coordinates.
For example:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell)
{
Console.WriteLine(hit.RowIndex);
}
}
}
I change the selection in the CellContextMenuStripNeeded event and then use the SelectedRows member to find it.
private void dataGridView_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
{
var Dgv = sender as DataGridView;
if (Dgv != null)
{
// Change the selection to reflect the right-click
Dgv.ClearSelection();
Dgv.Rows[e.RowIndex].Selected = true;
}
}
private void myToolStripMenuItem_Click(object sender, EventArgs e)
{
// Now pick up the selection as we know this is the row we right-clicked on
if (dataGridView.SelectedRows.Count > 0)
{
DoSomethingAmazing(dataGridView.SelectedRows[0]);
}
}
This also has the desired effect of highlighting a row that you r-click on.

Drag and Drop between 2 list boxes

Trying to implement drag and drop between 2 listboxes and all examples I've seen so far don't really smell good.
Can someone point me to or show me a good implementation?
Here's a sample form. Get started with a new WF project and drop two list boxes on the form. Make the code look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
listBox1.Items.AddRange(new object[] { "one", "two", "three" });
listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
listBox1.MouseMove += new MouseEventHandler(listBox1_MouseMove);
listBox2.AllowDrop = true;
listBox2.DragEnter += new DragEventHandler(listBox2_DragEnter);
listBox2.DragDrop += new DragEventHandler(listBox2_DragDrop);
}
private Point mDownPos;
void listBox1_MouseDown(object sender, MouseEventArgs e) {
mDownPos = e.Location;
}
void listBox1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) return;
int index = listBox1.IndexFromPoint(e.Location);
if (index < 0) return;
if (Math.Abs(e.X - mDownPos.X) >= SystemInformation.DragSize.Width ||
Math.Abs(e.Y - mDownPos.Y) >= SystemInformation.DragSize.Height)
DoDragDrop(new DragObject(listBox1, listBox1.Items[index]), DragDropEffects.Move);
}
void listBox2_DragEnter(object sender, DragEventArgs e) {
DragObject obj = e.Data.GetData(typeof(DragObject)) as DragObject;
if (obj != null && obj.source != listBox2) e.Effect = e.AllowedEffect;
}
void listBox2_DragDrop(object sender, DragEventArgs e) {
DragObject obj = e.Data.GetData(typeof(DragObject)) as DragObject;
listBox2.Items.Add(obj.item);
obj.source.Items.Remove(obj.item);
}
private class DragObject {
public ListBox source;
public object item;
public DragObject(ListBox box, object data) { source = box; item = data; }
}
}
the proper way to do a drag-drop control in .net is by running code in the 2nd control's DragDrop event handler.
It may "smell" weird, but this is how it works in .NET.
Google gave this: http://www.codeproject.com/KB/dotnet/csdragndrop01.aspx
It seems a pretty reasonable tutorial. If it smells bad, I think that's more to do with the API for drag and drop being awkward to use rather than the tutorial itself being poor.

Change the background color of Winform ListView headers

How can you change the background color of the Headers of a ListView?
You can do this by setting the OwnerDraw property for the list view to true.
This then allows you to provide event handlers for the listview's draw events.
There is a detailed example on MSDN
Below is some example code to set the header colour to red:
private void listView1_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, e.Bounds);
e.DrawText();
}
I think (but am happy to be proved wrong) that with OwnerDraw set to true, you will need to also provide handlers for the other draw events that have default implementations as shown below:
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
I certainly haven't managed to make the listview draw the items without that.
I know this is a little late to the party but I still saw this post and this would have helped me. Here is a little abstracted application of the code david supplied
using System.Windows.Forms;
using System.Drawing;
//List view header formatters
public static void colorListViewHeader(ref ListView list, Color backColor, Color foreColor)
{
list.OwnerDraw = true;
list.DrawColumnHeader +=
new DrawListViewColumnHeaderEventHandler
(
(sender, e) => headerDraw(sender, e, backColor, foreColor)
);
list.DrawItem += new DrawListViewItemEventHandler(bodyDraw);
}
private static void headerDraw(object sender, DrawListViewColumnHeaderEventArgs e, Color backColor, Color foreColor)
{
using (SolidBrush backBrush = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(backBrush, e.Bounds);
}
using (SolidBrush foreBrush = new SolidBrush(foreColor))
{
e.Graphics.DrawString(e.Header.Text, e.Font, foreBrush, e.Bounds);
}
}
private static void bodyDraw(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
Then call this in your form constructor
public Form()
{
InitializeComponent();
*CLASS NAME*.colorListViewHeader(ref myListView, *SOME COLOR*, *SOME COLOR*);
}
Just replace the *CLASS NAME* with whatever class you put the first bit of code in and the *SOME COLOR*'s with some sort of color.
//Some examples:
Color.white
SystemColors.ActiveCaption
Color.FromArgb(0, 102, 255, 102);

Keep a Silverlight Combobox DropDown open after losing focus

Is it possible to avoid the automatic collapse of a Silverlight ComboBox after LostFocus?
Well, looking at the disassembled code it looks like
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
this.FocusChanged(this.HasFocus());
}
is a good candidate for overwritting.
There's no way to solve your problem without implementing your own control subclass.
I've done the same to have a ComboBox with a Popup that doesn't close when I select an item (I want to have a multi-select behaviour).
If anyone is interested, here are my classes (works just fine for me as it is):
public class ComboBoxWithMultiSelect : ComboBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (base.IsDropDownOpen &&
(e.Key == Key.Enter ||
e.Key == Key.Space))
{
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
protected override DependencyObject GetContainerForItemOverride()
{
return new ComboBoxItemWithMultiSelect();
}
}
public class ComboBoxItemWithMultiSelect : ComboBoxItem
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
if (e == null)
{
throw new ArgumentNullException("e");
}
if (!e.Handled)
{
e.Handled = true;
}
}
}
I don't think there is an easy way around this. The code below is copied from the disassembled code from the ComboBox Class. As you can see it closes always when hasFocus is false. I don't think there is any way around this. Writing your own ComboBox is a solution.
private void FocusChanged(bool hasFocus)
{
this.UpdateSelectionBoxHighlighted();
base.SetValueInternal(IsSelectionActiveProperty, hasFocus, true);
if (!hasFocus)
{
this.IsDropDownOpen = false;
}
}

Resources