Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Retrieve Data From DataGridViewComboBoxColumn And Display In TextBox
 

Retrieve Data From DataGridViewComboBoxColumn And Display In TextBox

May i know how to retrieve a particular row of DataGridViewComboBoxColumn selected value and display it in textbox? Thank you.
lucerias  Sunday, January 21, 2007 2:31 PM
Maybe this will help.
vkh75  Sunday, January 21, 2007 3:15 PM
Hi follow is my sample, hope it would help you
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxColumn comboBoxColumn =new DataGridViewComboBoxColumn();
comboBoxColumn.Items.AddRange(
Color.Red, Color.Yellow, Color.Green, Color.Blue);
comboBoxColumn.ValueType =
typeof(Color);
dataGridView1.Columns.Add(comboBoxColumn);
dataGridView1.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
((
ComboBox)e.Control).SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
((
ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
this.textBox1.Text = ((ComboBox)sender).SelectedItem.ToString();
}
}
}
Bob zhu - SJTU  Monday, January 22, 2007 1:44 AM
I have tried to make use of this code together with DataGridViewComboColumn and however it doesn't work. Below is my code and please help me to check what is the problem. What i want to do is actually pass each ComboBox value to defined string and store it in database. May i know how to do it. Thank you.
 

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim cbo As New DataGridViewComboBoxColumn

With cbo

.Items.AddRange("Pending", "Approved", "Disapproved")

End With

DataGridView1.Columns.Insert(0, cbo)

 

End Sub

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

If e.Control.GetType() Is GetType(DataGridViewComboBoxEditingControl) Then

 

Dim cbo As ComboBox = e.Control

RemoveHandler cbo.SelectionChangeCommitted, AddressOf cboOnSelectionChangeCommitted

AddHandler cbo.SelectionChangeCommitted, AddressOf cboOnSelectionChangeCommitted

End If

End Sub

Private Sub cboOnSelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)

Try

Dim cbo As ComboBox = CType(sender, ComboBox)

TextBox1.Text = cbo.SelectedValue.ToString

Catch err As System.Exception

MessageBox.Show(err.Message)

End Try

End Sub

End Class

lucerias  Tuesday, January 23, 2007 5:31 AM

Replace this
TextBox1.Text = cbo.SelectedValue.ToString
with
TextBox1.Text = cbo.SelectedItem.ToString

vkh75  Tuesday, January 23, 2007 1:35 PM

You can use google to search for other answers

Custom Search

More Threads

• Suggestions for Remote Database Grid display in Web Page
• SQL HELP
• Datagridview Image Column
• Dynamic Fields in Crystal Report
• Create 1 dataset from 2 datasets and showing in Datagridview
• DataGridView ComboBoxcolumn binding to System.String[]
• DataGridView Columns
• Web Service error
• Bindingsource.Datasource Error in design
• Consuming a VS2005 Web Service that returns a data set