How can I get the selected text in an active gridcell
You need to get the DataGridTextBoxColumn.TextBox member, and retrieve the SelectedText from it for the active cell. The sample shows how you can do this as part of handling this TextBox’s rightClick. This code assumes you have specifically added DataGridTextBoxColumn for each column style. You can also download a VB sample. private void HandleMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { DataGridTableStyle ts = dataGrid1.TableStyles[”customers”]; DataGridTextBoxColumn cs = (DataGridTextBoxColumn)ts.GridColumnStyles[dataGrid1.CurrentCell.ColumnNumber]; MessageBox.Show(”Selected: ” + cs.TextBox.SelectedText); } }
How do I hide the gridlines or set them to a particular color
You can download a working project. Below are VB code snippets showing how you might do these tasks. ’load a bitmap from an embedded resource Dim Bmp As Bitmap ’ from an embedded resource Dim curName As String = ”BitmapVB.sync.bmp” Dim strm As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream(curName) Bmp = New Bitmap(strm) PictureBox1.Image = Bmp ….. ….. ’load a bitmap from a file Dim Bmp As Bitmap = Image.FromFile(”c:\sync.bmp”) PictureBox1.Image = Bmp ….. ….. ’modify a bitmap Dim Bmp As Bitmap = PictureBox1.Image.Clone Dim g As Graphics = Graphics.FromImage(Bmp) Dim brush1 As SolidBrush = New SolidBrush(Color.Red) g.DrawString(TextBox1.Text, TextBox1.Font, brush1, 10, 10) PictureBox2.Image = Bmp g.Dispose() …. …. ’save a bitmap as a file Dim dlg As SaveFileDialog = New SaveFileDialog() dlg.Title = ”Save BMP file” dlg.InitialDirectory = ”c:\” dlg.Filter = ”bmp files (*.bmp)|*.bmp|All files (*.*)|*.*” If dlg.ShowDialog = DialogResult.OK Then PictureBox2.Image.Save(dlg.FileName End If
How can I bind an ArrayList to a DataGrid
Here is a technique for binding an arraylist of objects where the objects contain public property that can appear as columns in the datagrid. In this example, the object contains 2 public doubles, one named value and the other named sqrt. To bind this arraylist to a datagrid, add a custom tablestyle that has a MappingName of ”ArrayList”, and then use the property names as the MappingName for each column. Below are some code snippets. You can download a working project that also has code to delete rows and add new rows to the bound arraylist. private void Form1_Load(object sender, System.EventArgs e) { CreateArrayList(); BindArrayListToGrid(); } private void BindArrayListToGrid() { dataGrid1.DataSource = arrayList1; //create a custom tablestyle and add two columnstyles DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = ”ArrayList”; int colwidth = (dataGrid1.ClientSize.Width – ts.RowHeaderWidth – SystemInformation.VerticalScrollBarWidth – 5) / 2; //create a column for the value property DataGridTextBoxColumn cs = new DataGridTextBoxColumn(); cs.MappingName = ”value”; //public property name cs.HeaderText = ”Random Number”; cs.Format = ”f4”; cs.Width = colwidth; ts.GridColumnStyles.Add(cs); //create a column for the sqrt property cs = new DataGridTextBoxColumn(); cs.MappingName = ”sqrt”; //public property name cs.HeaderText = ”Square Root”; cs.Format = ”f4”; cs.Width = colwidth; ts.GridColumnStyles.Add(cs); dataGrid1.TableStyles.Clear(); dataGrid1.TableStyles.Add(ts); } private void CreateArrayList() { arrayList1 = new ArrayList(); //add some items Random r = new Random(); for (int i = 0; i < 20; ++i) arrayList1.Add(new RandomNumber(r.NextDouble())); } //create a struct or class that defines what you want in each row //the different columns in the row must be public properties public struct RandomNumber { private double number; public RandomNumber(double d) { number = d; } public double value { get{ return number; } set{ number = value;} } public double sqrt { get {return Math.Sqrt(this.value);} } }
How can I get notification of the changing of a value in a column of comboboxes within my datagrid
This solution is based off the combobox for datagrid columns found in this FAQ. That solution replaces the standard textbox with a combobox. To get notifications of the changes, a delegate is passed into the constructor for the custom column style. This delegate is called anytime the combobox value changes. It passes the row number and value as arguments. You can download sample code (C#, VB) that shows the implementation.
What should I do to make my StringCollection property editable during design-time?
Use this custom editor attribute for the property that is of the StringCollection type (this can be used for other Collection types too if you want to allow entry of strings into the collection during design-time). Editor(‘System.Windows.Forms.Design.StringCollectionEditor, System.Design’, ‘System.Drawing.Design.UITypeEditor, System.Drawing’), public YourCollection YourProp{get{…}set{…}} StringCollectionEditor is not public so you have to use the above constructor override and specify the typename. This editor should allow you to edit your collection without any problems.