Get selected/highlighted Text out of GridView - c#

When I highlight/mark a text with my mouse in a GridView, is it possible to get access to this selected Text? I want to save it in a string variable when I click for example on a button. It's not important if I get that text via a GridView access or something else, I need simply the marked text. I tried to trigger ctrl+c by clicking the button to safe it in clipboard, but I had problems with System.Windows.Forms. I didn't find any events like gridview.GetSelectedText for example.
I made a controller and first of all I want to get access to the selected text in the gridView:
namespace XPress.Module.Controllers
{
public class TransactionAddStringToCategoryKeywordController : ObjectViewController<ObjectView, Transaction>
{
private const string addStringToCategoryKeywordId = "AddStringToCategoryKeyword";
private readonly SimpleAction addStringToCategoryKeyword;
public TransactionAddStringToCategoryKeywordController()
{
addStringToCategoryKeyword = new SimpleAction(this, addStringToCategoryKeywordId, DevExpress.Persistent.Base.PredefinedCategory.Edit);
addStringToCategoryKeyword.ImageName = "Action_Export";
//addStringToCategoryKeyword.TargetObjectsCriteria = "Not IsNullOrEmpty([NameFull])";
addStringToCategoryKeyword.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
addStringToCategoryKeyword.Execute += TransactionAddStringToCategoryKeyword_Execute;
}
protected override void OnActivated()
{
base.OnActivated();
addStringToCategoryKeyword.Active["test"] = true;
}
}
}
And I have access to the GridView:
private void WinAlternatingRowsController_ViewControlsCreated(object sender, EventArgs e)
{
GridListEditor listEditor = ((ListView)View).Editor as GridListEditor;
if (listEditor != null)
{
DevExpress.XtraGrid.Views.Grid.GridView gridView = listEditor.GridView;
}
}

Related

How can I hide the current cell position on a DataGridView control?

I have a DataGridView control that allows for multi row selecting. The issue I have is with the current position of the selection being shown (see image below). I would like to NOT have the current cell position shown at all. Is that possible?
I would like it to look like the following instead...
Derive a class from DataGridView and override the ShowFocusCues property.
Return True to show the focus rectangle.
Return False to hide the focus rectangle.
Return base.ShowFocusCues to maintain default behavior.
You can also expose a public property to change it dynamically.
public class DataGridViewFocused : DataGridView
{
public bool? ShowFocus { get; set; }
protected override bool ShowFocusCues
{
get
{
return this.ShowFocus.HasValue? this.ShowFocus.Value : base.ShowFocusCues;
}
}
}
Adding it to your project to replace any existing DataGridView can be as simple as navigating into your Form.Designer.cs file and replacing the following:
public System.Windows.Forms.DataGridView dataGridView1;
this.dataGridView1 = new System.Windows.Forms.DataGridView();
with:
public DataGridViewFocused dataGridView1;
this.dataGridView1 = new DataGridViewFocused();
From there you can always hide the focus rectangle by adding the following line:
this.dataGridView1.ShowFocus = false;
Or, for example, if you wanted to hide that rectangle only during a multiple-select event, you could do something like the following:
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedCells.Count > 1)
{
this.dataGridView1.ShowFocus = false;
}
else
{
this.dataGridView1.ShowFocus = null;
}
}

variable and textbox mapping in c# without using textbox changed event

I'm trying a code which changes the TextBox values when variable mapped with it changes accordingly without using TextBox changed event. I am not finding any clue to where to start please help me.
Here is the code:
public void varChange(TextBox text)
{
String name;
name="sachin";
text.Text = name;
MessageBox.Show("" + text.Text);
}
You can "extend" TextBox :
public class MeTextBox : TextBox
{
public override string Text
{
get
{
return base.Text;
}
set
{
//base.Text = value; // use it or not .. whatever
MyTextWasChanged();
}
}
void MyTextWasChanged()
{
String name;
name="sachin";
//text.Text = name;
base.Text = name;
MessageBox.Show("" + text.Text);
}
}
If that's not what you're looking for then give some more details and I'll update this answer.
You can use a BindingSource
public partial class Form1 : Form
{
private System.Windows.Forms.BindingSource form1BindingSource;
public string BindedProp { get; set; } //Variable or property binded with TextBox
public Form1()
{
InitializeComponent();
this.form1BindingSource = new System.Windows.Forms.BindingSource(new System.ComponentModel.Container());
this.form1BindingSource.DataSource = typeof(binding.Form1);
this.textBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.form1BindingSource, "BindedProp", true));
this.form1BindingSource.DataSource = this;
}
//add a button control to assing value code event click
private void btAssingValueProperty_Click(object sender, EventArgs e)
{
BindedProp = "Value assigned";
form1BindingSource.ResetBindings(false);
}
//add a other button control to show value code event click
private void btShowValueProperty_Click(object sender, EventArgs e)
{
MessageBox.Show(BindedProp);
}
}

Adding XML node on the basis of TextBox entry inside WPF Form

Suppose i have created a WPF form having one text box. i am calling that form inside another wpf window's gridpanel and after entering value inside the textbox, i am clicking on submit button. After button click, i need to get that value and save it in string form inside my current class. My logic is something like this.
For getting the from inside my current window:-
void SelectedClick(object sender, RoutedPropertyChangedEventArgs<object> e)
{
selectedItem.ContextMenu = VcontextMenu;
VcontextMenu.Items.Add(VmenuItem1);
VmenuItem1.Click += AddValidation;
details();
}
void AddValidation(object sender, RoutedEventArgs e)
{
ValidationForm obj = new ValidationForm();
ProcessGrid.Content = obj.VForm;
}
Now i want to store the value of my textbox inside a string. For that i have used following code:-
public void details()
{
ValidationForm obj = new ValidationForm();
string str = obj.s.ToString();
}
My ValidationForm Code:-
public partial class ValidationForm : UserControl
{
public string s { get; set; }
public ValidationForm()
{
InitializeComponent();
}
public void XSave_Click(object sender, RoutedEventArgs e)
{
s = TextValidationName.Text;
}
}
but instead of opening the form, the control is going to obj.s.ToString() and showing error as "Object reference not set to an instance of an object." Please help. Thanks.
The issue is caused since the string s in your ValidationForm class is not assigned. It is probably caused since the XSave_Click() method is not being called.
Ensure that you properly assign the value to s before you try to get value from it.

How to pass data in combo box in one form to the gridview in another form in windows application using C#.net

I have a gridview in one form and a combo box in another form.If i select a value in the combo box that value should be passed to the gridview.how can i achieve this...
Your question has too few details, but as a suggestion I'll try to put some code and maybe it'll give you some hint.
// In case if you're dealing with WinForms
// For WPF solution could have similar approach
public class DataGridForm : Form
{
private DataGrid _grid;
public DataGridForm()
{
InitializeComponent();// <-- here _grid is instantiated
}
public void LoadData(object data)
{
// load data into grid
// I don't know, could be smt like
DataGridCell cell = _grid.Cells[0,0];
cell.Value = data;
}
}
public class FormWithComboBox : Form
{
private ComboBox _comboBox;
public DataGridForm DataGridForm { get; set; }// value is set by some externat user
public FormWithComboBox()
{
InitializeComponent();// <-- here _grid is instantiated
// including handler for OnSelectedItemChanged event
}
private void _comboBox_OnSelectedItemChanged(object sender, EventArgs e)
{
DataGridForm.LoadData(_comboBox.SelectedItem);
}
}

DataBinding in winforms : Update even though I click "Cancel"

I have a problem with DataBinding in Winforms, Even though I click "Cancel" on the form, the objecte is updated.
I've set the property "DialogResult" of the Ok button to "OK", of the Cancel button to "Cancel", also, I've set the properties "AccesptButton" and "CancelButton" of the form to bnOk and bnCancel.
Here is my code :
Model :
private string code;
public string Code
{
get { return code; }
set { SetPropertyValue<string>("Code", ref code, value); }
}
private string libelle;
public string Libelle
{
get { return libelle; }
set { SetPropertyValue<string>("Libelle", ref libelle, value); }
}
UI :
public FamilleTiers CurrentFamilleTiers { get; set; }
private void FamilleTiersForm_Load(object sender, EventArgs e)
{
txCode.DataBindings.Add("Text", CurrentFamilleTiers, "Code");
txLibelle.DataBindings.Add("Text", CurrentFamilleTiers, "Libelle");
}
Edit function :
public static void EditFamilleTiers(FamilleTiers selectedFamilleTiers)
{
using (FamilleTiersForm form = new FamilleTiersForm() { CurrentFamilleTiers = selectedFamilleTiers, Text = selectedFamilleTiers.Libelle })
{
if (form.ShowDialog() == DialogResult.OK)
{
form.CurrentFamilleTiers.Save();
}
}
}
Thanks for your time
When you click cancel on a form data binding does not revert you need to keep a backup copy of the values and if they change replace the new values with the original values. .Net does not know what your wanting to do.

Categories