如何在更改时保存数据网格中的列顺序? [英] How Can I Save The Column Order In A Datagrid When Changed?

查看:65
本文介绍了如何在更改时保存数据网格中的列顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个datagrid,在用户更改时保存列顺序,创建一个xml文件,当用户重新打开程序时,它会读取xml文件并使用配置打开它。 />




提前致谢

I would like to make a datagrid that saves the column order when is changed by the user , creating an xml file and when the user re-open the program , it reads the xml file and opens it with the configuration.


Thanks in advance

推荐答案

这是我的整个程序......我只想保存datagrid的列顺序:



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq; <使用System.Text;
使用System.Windows.Forms;

使用MySql.Data.MySqlClient;

使用System.Xml;

使用System.Collections.Specialized;







命名空间WindowsFormsApplication4

{











公共部分班级表格1:表格

{

private MySqlConnection conn = null;

private MySqlDataAdapter da = null;

private DataSet ds = null;

私有字符串Back_Color_active =#FFADEFD1;

私有字符串Back_Color_inactive =#FFFF0000;

private int rowcnt = 0;

私有DataGridView dataGridView1 = new DataGridView();

public Size Size {get;组; }





public Form1()

{

InitializeComponent();

















Grid1.BackgroundColor = System.Drawing.Color.White;



string cs = @server = *****; userid = *****;

password = ******; database = ******;



string stm =SELECT userid,username,userlast,profile,status from states;



试试

{

conn = new MySqlConnection(cs);

conn.Open();

ds = new DataSet();

da = new MySqlDataAdapter(stm,conn);

da.Fill(ds,Users);



Grid1.DataSource = ds.Tables [用户];
Grid1.BorderStyle = BorderStyle.FixedSingle;



}

catch(MySqlException ex)

{

//Console.WriteLine(\"Error:\"+ ex.ToString());

MessageBox.Show(Error:+ ex.ToString ());

}

终于

{

if(conn!= null)

{

conn.Close();



}



}



}



//私人DataGridViewCell clickedCell;







private void Grid1_CellContentDoubleClick(object sender,DataGridViewCellEventArgs e)

{







// Form3 f3 =新Form3();

//f3.username.Text = Grid1.Rows [e.RowIndex] .Cells [username]。Value.ToString();

//f3.userlast.Text = Grid1.Rows [e.RowIndex] .Cells [ userlast]。Value.ToString();



//f3.Show();

}



private void Grid1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)

{



rowcnt = ds.Tables [ 0] .Rows.Count - 1;



for(int i = 0;我< = rowcnt; i ++)

{







// MessageBox.Show(rowcnt .ToString());



if(Grid1.Rows [i] .Cells [status]。Value.ToString()==I)

{

Grid1.Rows [i] .DefaultCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(Back_Color_inactive);

}

else

{

Grid1.Rows [i] .DefaultCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(Back_Color_active);



}

}



}



private void button2_Click(object sender,EventArgs e)

{

Form3 f3 = new Form3();

f3.Show ();

}



private void button1_Click(对象发送者, EventArgs e)

{



//MessageBox.Show(你确定要关闭这个应用程序吗?);

this.Close();



}



private void Form1_Load(object发件人,EventArgs e)

{



// DataSet flatDataSet = new DataSet();

// flatDataSet.ReadXml(@C:\Users\rs\Desktop\Save\save.xml);

// DataTable table = flatDataSet.Tables [0];

//Grid1.DataSource = table;







}



private void Form1_FormClosed(object sender,FormClosedEventArgs e)

{

// if(MessageBox.Show(This将关闭整个应用程序。确认?,关闭应用程序,MessageBoxButtons.YesNo)== DialogResult.Yes)

{

// MessageBox.Show(应用程序已成功关闭。 ,申请已结束!,MessageBoxButtons.OK);



}

//其他

{

//e.Cancel = true;

// this.Activate();

}

< br $>


类MForm:表格

{



private DataGrid dg = null;

private MySqlConnection conn = null;

private MySqlDataAdapter da = null;

private DataSet ds = null;



public MForm()

{



this.Text =DataGrid;

this.Size = new Size(350,300);



this.InitUI();

this.I nitData();



this.CenterToScreen();

}



void InitUI()

{

dg = new DataGrid();



dg.CaptionBackColor = System .Drawing.Color.White;

dg.CaptionForeColor = System.Drawing.Color.Black;

dg.CaptionText =Users;



dg.Location = new Point(8,0);

dg.Size = new Size(350,300);

dg .TabIndex = 0;

dg.Parent = this;

}



void InitData()

{

string cs = @server = *******; userid = *******;

password = ********;数据库= *********;



string stm =SELECT * db;





试试

{

conn = n ew MySqlConnection(cs);

conn.Open();

ds = new DataSet();

da = new MySqlDataAdapter(stm, conn);

da.Fill(ds,Users);



dg.DataSource = ds.Tables [Users] ;



}

catch(MySqlException ex)

{

控制台。 WriteLine(错误:+ ex.ToString());



}

终于

{

if(conn!= null)

{

conn.Close();

}

}

}

}
This is my whole program.... i just want to save the column order of the datagrid :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Xml;
using System.Collections.Specialized;



namespace WindowsFormsApplication4
{





public partial class Form1 : Form
{
private MySqlConnection conn = null;
private MySqlDataAdapter da = null;
private DataSet ds = null;
private string Back_Color_active = "#FFADEFD1";
private string Back_Color_inactive = "#FFFF0000";
private int rowcnt = 0;
private DataGridView dataGridView1 = new DataGridView();
public Size Size { get; set; }


public Form1()
{
InitializeComponent();








Grid1.BackgroundColor = System.Drawing.Color.White;

string cs = @"server=*****;userid=*****;
password=******;database=******";

string stm = "SELECT userid,username,userlast,profile,status from ******";

try
{
conn = new MySqlConnection(cs);
conn.Open();
ds = new DataSet();
da = new MySqlDataAdapter(stm, conn);
da.Fill(ds, "Users");

Grid1.DataSource = ds.Tables["Users"];
Grid1.BorderStyle = BorderStyle.FixedSingle;

}
catch (MySqlException ex)
{
//Console.WriteLine("Error: " + ex.ToString());
MessageBox.Show("Error: " + ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();

}

}

}

//private DataGridViewCell clickedCell;



private void Grid1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{



//Form3 f3 = new Form3();
//f3.username.Text = Grid1.Rows[e.RowIndex].Cells["username"].Value.ToString();
//f3.userlast.Text = Grid1.Rows[e.RowIndex].Cells["userlast"].Value.ToString();

//f3.Show();
}

private void Grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

rowcnt = ds.Tables[0].Rows.Count - 1;

for (int i = 0; i <= rowcnt; i++)
{



// MessageBox.Show(rowcnt.ToString());

if (Grid1.Rows[i].Cells["status"].Value.ToString() == "I")
{
Grid1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(Back_Color_inactive);
}
else
{
Grid1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(Back_Color_active);

}
}

}

private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}

private void button1_Click(object sender, EventArgs e)
{

//MessageBox.Show("Are you sure you want to close this application?");
this.Close();

}

private void Form1_Load(object sender, EventArgs e)
{

// DataSet flatDataSet = new DataSet();
//flatDataSet.ReadXml(@"C:\Users\rs\Desktop\Save\save.xml");
//DataTable table = flatDataSet.Tables[0];
//Grid1.DataSource = table;



}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);

}
// else
{
//e.Cancel = true;
// this.Activate();
}


class MForm : Form
{

private DataGrid dg = null;
private MySqlConnection conn = null;
private MySqlDataAdapter da = null;
private DataSet ds = null;

public MForm()
{

this.Text = "DataGrid";
this.Size = new Size(350, 300);

this.InitUI();
this.InitData();

this.CenterToScreen();
}

void InitUI()
{
dg = new DataGrid();

dg.CaptionBackColor = System.Drawing.Color.White;
dg.CaptionForeColor = System.Drawing.Color.Black;
dg.CaptionText = "Users";

dg.Location = new Point(8, 0);
dg.Size = new Size(350, 300);
dg.TabIndex = 0;
dg.Parent = this;
}

void InitData()
{
string cs = @"server=*******;userid=*******;
password=********;database=*********";

string stm = "SELECT * db";


try
{
conn = new MySqlConnection(cs);
conn.Open();
ds = new DataSet();
da = new MySqlDataAdapter(stm, conn);
da.Fill(ds, "Users");

dg.DataSource = ds.Tables["Users"];

}
catch (MySqlException ex)
{
Console.WriteLine("Error: " + ex.ToString());

}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}


这篇关于如何在更改时保存数据网格中的列顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆