Code Block
namespace DGV
{
public partial class Form15 : Form
{
public Form15()
{
InitializeComponent();
}
private void Form15_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("testtable");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("dateT", typeof(DateTime));
for (int i = -5; i < 6; i++)
{
dt.Rows.Add(i,DateTime.Now.AddDays(i));
}
dt.AcceptChanges();
this.dataGridView1.DataSource = dt.DefaultView;
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
dt.WriteXml(@"C:\test\text.xml");
}
}
}
For your second question, how to delete the cells information values only when pressing the Delete key? You need to create a custom DataGridView inherit from the standard DataGridView and override the ProcessCmdKey method.
Code Block
class CustomDGV : DataGridView
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Delete)
{
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}