Windows Develop Bookmark and Share   
 index > Windows Forms Designer > how to display values from selected row of datagridview into text boxes?
 

how to display values from selected row of datagridview into text boxes?

i created a windows form app with a datagrid view in it.
after selecting a row in the datagridview, by clicking a btn (bn_edit) it is supposed to populate the values into a set of textboxes inside
the form after de-serialization.
the values in the datagridview are obtained after deserialization of an xml file. how do i do it?
aysrun  Friday, April 24, 2009 5:44 AM

Hi aysrun,

How did you populate the DataGridView rows? Did you use data binding? Here is a very easy way to populate data from xml file and save data to it using data binding.
public partial class Form1 : Form
{
private DataTable dtSource = new DataTable("Table1");

public Form1()
{
InitializeComponent();
dtSource.Columns.Add("Column1", typeof(int));
dtSource.Columns.Add("Column2", typeof(string));
dtSource.ReadXml(@"D:\data.xml");
dataGridView1.DataSource = dtSource;

textBox1.DataBindings.Add("Text", dtSource, "Column1");
textBox2.DataBindings.Add("Text", dtSource, "Column2");
}
private void btnSave_Click(object sender, EventArgs e)
{
dtSource.WriteXml(@"D:\data.xml");
}
}
In this example, I use a DataTable, WriteXml and ReadXml will auto serialize/deserialize the data into/from an xml file.

Then bind the dtSource to the TextBox, when you click different rows, it will populate the selected rows data in the two textboxes.

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, April 27, 2009 6:25 AM
Hi,

So, do you have problem in de-serializing?
Do you have any issues in directly reading the data from DataGrid Row and fill it in the Textbox?

Please feel free to ask.
Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
Abhijeet Lakra  Friday, April 24, 2009 6:06 AM

for the deserializing part, it only serializes the first row of the datagridview.

im hoping to make it in a way that the only the selected row will be deserialized and displayed in the textboxes after user
clicks an EDIT button (bn_edit) not just the first row.

aysrun  Friday, April 24, 2009 6:24 AM

Hi aysrun,

How did you populate the DataGridView rows? Did you use data binding? Here is a very easy way to populate data from xml file and save data to it using data binding.
public partial class Form1 : Form
{
private DataTable dtSource = new DataTable("Table1");

public Form1()
{
InitializeComponent();
dtSource.Columns.Add("Column1", typeof(int));
dtSource.Columns.Add("Column2", typeof(string));
dtSource.ReadXml(@"D:\data.xml");
dataGridView1.DataSource = dtSource;

textBox1.DataBindings.Add("Text", dtSource, "Column1");
textBox2.DataBindings.Add("Text", dtSource, "Column2");
}
private void btnSave_Click(object sender, EventArgs e)
{
dtSource.WriteXml(@"D:\data.xml");
}
}
In this example, I use a DataTable, WriteXml and ReadXml will auto serialize/deserialize the data into/from an xml file.

Then bind the dtSource to the TextBox, when you click different rows, it will populate the selected rows data in the two textboxes.

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, April 27, 2009 6:25 AM

hi kira,

thanks fer you help. it is quite useful.
but there seemed to have some changes in the requirements.
after de-serializing the xml, i made it in a way that it is contained in a hashtable.
this hashtable is then displayed in the datagridview successfully.
now i want to make in a way that after i select one row in the datagridview, it is supposed to populate the values into the
textboxes.
how do i do that?

here are the codes which populates the hashtable into the datagridview:

Hashtable

hashOI = new Hashtable();
populateOIDataGrid(hashOI)


private

hashOI = new Hashtable();
populateOIDataGrid(hashOI)


private

hashOI = new Hashtable();
populateOIDataGrid(hashOI)



private



private

void populateOIDataGrid(Hashtable hashtable)
{

DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("OrderItem");

dt.Columns.Add(

"OrderItemRowId", typeof(string));
dt.Columns.Add(
"OrderItemCode", typeof(string));

IDictionaryEnumerator enumerator = hashtable.GetEnumerator();

DataRow row = null;

while (enumerator.MoveNext())
{

ORDITEM oi = (ORDITEM)enumerator.Value;
row = dt.NewRow();
row[
"OrderItemRowId"] = oi.OrderItemRowId;
row[
"OrderItemCode"] = oi.OrderItemCode;
dt.Rows.Add(row);

}

dataGridView1.DataSource = ds.Tables[0];

}

aysrun  Thursday, May 07, 2009 2:25 AM

You can use google to search for other answers

Custom Search

More Threads

• How to obtain a member field declaration WithEvents using CodeDom
• Need some help with an Enum TypeConverter, I think
• Serializing a Collection
• Is there any chance to modify contents of Form InitializeComponent permanently using Designer?
• DragDrop Operation at Runtime
• Collection Property
• Hiding the Script Combobox in FONT Dialog
• How to code while pressing the Ctrl+Tab Key in Windows Form?
• Splitter control on event
• Tooltip can't display in MDI form design.