Hi I would like to ask a solution where i can get the whole row data or multiple rows from a datagridview if the checkbox is checked and save it to an xml file
here is my exisiting code
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = ds.Tables["Tables"];
DataView view = new DataView(dt);
view.RowFilter = "MD_ID = " + MdNum;
//add checkbox to datagrid data
dataGridView2.Columns.Add(chk);
chk.HeaderText = "Select";
//populate datagridview with data
dataGridView2.DataSource = view;
}
private void SaveBtn_Click(object sender, EventArgs e)
{
}
my table has 4 columns MD_Num, MD_ID, MD_AGE, MD_DATE. is it possible to select a row and insert each value of the column to an individual textbox and save it to an XML file with the ff format:
<MDS>
<MD>
<Tables>
<MD_Num>VALUE#</MD_Num>
<MD_Num>VALUE#<MD_Num>
<MD_AGE>VALUE#<MD_AGE>
<MD_DATE>VALUE#<MD_DATE>
<Tables>
<MD>
</MDS>
Answering your second question... inside the cell click event for the dataGridView
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtnum.Text = dataGridView1.SelectedRows[0].Cells["MD_Num"].Value.ToString();
txtid.Text = dataGridView1.SelectedRows[0].Cells["MD_Num"].Value.ToString();
txtage.Text = dataGridView1.SelectedRows[0].Cells["MD_Age"].Value.ToString();
txtdate.Text = dataGridView1.SelectedRows[0].Cells["MD_Date"].Value.ToString();
}
for the save button, feel free to change the path of the xml file
private void btn_Save_Click(object sender, EventArgs e)
{
string num = txtnum.Text;
string id = txtid.Text;
string age = txtage.Text;
string date = txtdate.Text;
XmlTextWriter writer = new XmlTextWriter(@"C:\Users\Public\Desktop\Details.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("MDS");
create_node(num, id, age,date, writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
MessageBox.Show("XML File created ! ");
}
then the method you will have to add that creates the nodes
private void create_node(string num, string id, string age,string date, XmlTextWriter writer)
{
writer.WriteStartElement("Table");
writer.WriteStartElement("MD_Num");
writer.WriteString(num);
writer.WriteEndElement();
writer.WriteStartElement("MD_ID");
writer.WriteString(id);
writer.WriteEndElement();
writer.WriteStartElement("MD_Age");
writer.WriteString(age);
writer.WriteEndElement();
writer.WriteStartElement("MD_Date");
writer.WriteString(date);
writer.WriteEndElement();
writer.WriteEndElement();
}
ooh and under the InitializeComponent()
change selection mode to full row select
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.