Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
From Datagrid when i'm selecting row should should generate as xml in system

Regards
Balamurugan
Posted

1 solution

Coding :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace sample
{
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window
{
SqlConnection con = new SqlConnection("server=.;database=test;trusted_connection=true;");
public Window1()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
con.Open();
comboBox1.Items.Add("All");
comboBox2.Items.Add("All");
comboBox3.Items.Add("All");
comboBox4.Items.Add("All");
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 0;
SqlCommand cmd = new SqlCommand("select distinct gn from test", con);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
comboBox1.Items.Add(rd[0].ToString());
}
rd.Close();
cmd = new SqlCommand("select distinct cn from test", con);
rd = cmd.ExecuteReader();
while (rd.Read())
{
comboBox2.Items.Add(rd[0].ToString());
}

rd.Close();
cmd = new SqlCommand("select distinct at from test", con);
rd = cmd.ExecuteReader();
while (rd.Read())
{
comboBox3.Items.Add(rd[0].ToString());
}
rd.Close();
cmd = new SqlCommand("select distinct ft from test", con);
rd = cmd.ExecuteReader();
while (rd.Read())
{
comboBox4.Items.Add(rd[0].ToString());
}
rd.Close();
con.Close();
}

private void button2_Click(object sender, RoutedEventArgs e)
{
int i = 0;
string com = "select * from test";
if (comboBox1.Text != "All" || comboBox2.Text != "All" || comboBox3.Text != "All" || comboBox4.Text != "All")
{
com += " where ";
}
if (comboBox1.Text != "All")
{
if (i < 1)
{
com += "gn='" + comboBox1.Text + "' ";
i++;
}
else
{
com += "and gn='" + comboBox1.Text + "' ";
i++;
}
}
if (comboBox2.Text != "All")
{
if (i < 1)
{
com += "cn='" + comboBox2.Text + "' ";
i++;
}
else
{
com += "and cn='" + comboBox2.Text + "' ";
i++;
}
}
if (comboBox3.Text != "All")
{
if (i < 1)
{
com += "at='" + comboBox3.Text + "' ";
i++;
}
else
{
com += "and at='" + comboBox3.Text + "' ";
i++;
}
}
if (comboBox4.Text != "All")
{
if (i < 1)
{
com += "ft='" + comboBox4.Text + "' ";
i++;
}
else
{
com += "and ft='" + comboBox4.Text + "' ";
i++;
}
}
DataTable datatable = new DataTable();
datatable.Columns.Add("Select", typeof(bool));
datatable.Columns.Add("Group Name", typeof(string));
datatable.Columns.Add("Client Name", typeof(string));
datatable.Columns.Add("Assesment Type", typeof(string));
datatable.Columns.Add("Form Type", typeof(string));

con.Open();
SqlCommand cmd = new SqlCommand(com,con);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
DataRow tr = datatable.NewRow();
tr["Select"] = false;
tr["Group Name"] = rd[0].ToString();
tr["Client Name"] = rd[1].ToString();
tr["Assesment Type"] = rd[2].ToString();
tr["Form Type"] = rd[3].ToString();
datatable.Rows.Add(tr);
}
rd.Close();
con.Close();
dataGrid1.DataContext = datatable;
}

private void button1_Click(object sender, RoutedEventArgs e)
{
String str = "" + Environment.NewLine + "<node>" + Environment.NewLine;
for (int i = 0; i < dataGrid1.Items.Count; i++)
{
System.Data.DataRowView dv = (DataRowView)dataGrid1.Items[i];
DataRow dr = dv.Row;
if(dr.ItemArray[0].ToString()=="True")
{
str += "<record>" + Environment.NewLine;
str += "<" + dataGrid1.Columns[1].Header.ToString().Replace(" ", "_") + ">" + Environment.NewLine + dr.ItemArray[1].ToString() + Environment.NewLine + " + Environment.NewLine;
str += "<" + dataGrid1.Columns[2].Header.ToString().Replace(" ", "_") + ">" + Environment.NewLine + dr.ItemArray[2].ToString() + Environment.NewLine + " + Environment.NewLine;
str += "<" + dataGrid1.Columns[3].Header.ToString().Replace(" ", "_") + ">" + Environment.NewLine + dr.ItemArray[3].ToString() + Environment.NewLine + " + Environment.NewLine;
str += "<" + dataGrid1.Columns[4].Header.ToString().Replace(" ", "_") + ">" + Environment.NewLine + dr.ItemArray[4].ToString() + Environment.NewLine + " + Environment.NewLine;
str += "" + Environment.NewLine;
}
}
str += "";
System.IO.File.WriteAllText("G:\\text.xml", str);
MessageBox.Show("File Generated Successfully");
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900