请教如何将DATAGRID的列顺序怎么交换积分顺序

连接数据库一般步骤:
1、建立SqlConnection对象;
2、指定SqlConnection对象的ConnectionString 属性;
3、打开数据库连接;
4、指定SQL语句;
5、建立SqlDataAdapter对象和DataSet对象(myDataAdapter = new SqlDataAdapter(strCmd,conn);
6、添冲DataS
7、给DataGrid指定数据源
//创建sqlConnectin
//实例化sqlConnection对象
cnn = new SqlConnection("server=(local) ; database= uid = pwd = ****");
cnn.open();
//创建查询语句
String find = "select * from table";
//创建sqlDataAdapter
SqlDataAdapter myDa = new SqlDataAdapter(find , cnn);
//创建DataSet用来存储数据
DataSet DS = new DataSet();
//将数据填充到DataSet中
myDa.Fill(DS, "name");
//在DataGirdView中显示
myDataGirdView.DataSource = DS.Tables["name"];
c# DataGridView连接SQL Server或本地同步数据输出
C#连接MySQL数据库实现DataGridView定时更新数据——多结果集返回
[新手]C# winform 用dataGridView显示数据库内容
关于从数据库取数据,并显示在DataGridView中
c#sql server数据库与datagridview的绑定
c#数据库操作DataGridView控件的使用,ADO.NET
C# 的DataGridView 操作数据库 插入,更新,删除
C#客户端绑定DataView和DataTable的几个技巧
C#datagridview读取数据库数据并显示
没有更多推荐了,DataGridView两列进行互换的源码
private void toolLeft2_Click(object sender, EventArgs e)
& & & & & & int selcol = DBGrid2.CurrentCell.ColumnI
& & & & & & int selrow = DBGrid2.CurrentCell.RowI
& & & & & & DataGridViewColumn col = (DataGridViewColumn)DBGrid2.Columns[selcol].Clone();
& & & & & & DBGrid2.Columns.Insert(DBGrid2.CurrentCell.ColumnIndex - 1, col);
& & & & & & DBGrid2.Columns.RemoveAt(selcol + 1);
& & & & & & DBGrid2.CurrentCell = DBGrid2.Rows[selrow].Cells[selcol - 1];
& & & & private void toolRight2_Click(object sender, EventArgs e)
& & & & & & int selcol = DBGrid2.CurrentCell.ColumnI
& & & & & & int selrow = DBGrid2.CurrentCell.RowI
& & & & & & DataGridViewColumn col = (DataGridViewColumn)DBGrid2.Columns[selcol].Clone();
& & & & & & DBGrid2.Columns.Insert(DBGrid2.CurrentCell.ColumnIndex + 2, col);
& & & & & & DBGrid2.Columns.RemoveAt(selcol);
& & & & & & DBGrid2.CurrentCell = DBGrid2.Rows[selrow].Cells[selcol + 1];
& & & & private void DBGrid2_SelectionChanged(object sender, EventArgs e)
& & & & & & if (DBGrid2.CurrentCell == null)
& & & & & & if (DBGrid2.CurrentCell.ColumnIndex &= 1 || DBGrid2.CurrentCell.ColumnIndex &= DBGrid2.ColumnCount - 1)
& & & & & & {
& & & & & & & & toolLeft2.Enabled =
& & & & & & & & toolRight2.Enabled =
& & & & & & }
& & & & & & else if (DBGrid2.CurrentCell.ColumnIndex == 2)
& & & & & & {
& & & & & & & & toolLeft2.Enabled =
& & & & & & & & if (DBGrid2.CurrentCell.ColumnIndex == DBGrid2.ColumnCount - 2)
& & & & & & & & & & toolRight2.Enabled =
& & & & & & & & else
& & & & & & & & & & toolRight2.Enabled =
& & & & & & }
& & & & & & else
& & & & & & {
& & & & & & & & toolLeft2.Enabled =
& & & & & & & & if (DBGrid2.CurrentCell.ColumnIndex == DBGrid2.ColumnCount - 2)
& & & & & & & & & & toolRight2.Enabled =
& & & & & & & & else
& & & & & & & & & & toolRight2.Enabled =
& & & & & & }Silverlight的很多控件相对于VS的其他程序来说,还很不成熟,Datagrid就是其中一个,可能个人比较愚笨,经过好几天才把一些问题搞清楚了。
先描述一下自己这个测试程序的功能。
1,Datagrid上显示10条信息,在最后面另外显示一条空信息,当其中的内容被改变以后,这条信息存入数据源中,Datagrid的最后重新添加一行空信息
2,通过上下键实现数据的交换,比如当前选择的index是5,按了向下的箭头之后,5和6交换,光标跟随5到6的位置上。
3,添加,插入,删除数据操作
实现中出现的问题:
1,需要确定初始index,开始决定使用Datagrid的SelectionChanged事件来获取
private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
m_CurrentIndex = dg.SelectedI
然后在KeyUp事件中交换
private void dg_KeyUp(object sender, KeyEventArgs e)
//MessageBox.Show(dg.SelectedIndex.ToString());
if (e.Key == Key.Up)
if (m_CurrentIndex != dg.SelectedIndex)
Data tem = source[m_CurrentIndex - 1];
source[m_CurrentIndex - 1] = source[m_CurrentIndex];
source[m_CurrentIndex] =
//source.Insert(dg.SelectedIndex, source[m_CurrentIndex]);
//source.RemoveAt(m_CurrentIndex + 1);
dg.ItemsSource =
dg.ItemsSource = UpDateTempSource(source);
//m_CurrentIndex = dg.SelectedI
m_CurrentIndex = m_CurrentIndex - 1;
dg.SelectedIndex = m_CurrentI
if (e.Key == Key.Down)
if (m_CurrentIndex != dg.SelectedIndex)
if (dg.SelectedIndex & source.Count - 1)
Data tem = source[m_CurrentIndex + 1];
source[m_CurrentIndex + 1] = source[m_CurrentIndex];
source[m_CurrentIndex] =
//source.Insert(dg.SelectedIndex, source[m_CurrentIndex]);
//source.RemoveAt(m_CurrentIndex + 1);
dg.ItemsSource =
dg.ItemsSource = UpDateTempSource(source);
//m_CurrentIndex = dg.SelectedI
m_CurrentIndex = m_CurrentIndex + 1;
dg.SelectedIndex = m_CurrentI
问题出现了,每次按下上下键,SelectedIndex都会改变,因此在KeyUp中的这个判断if (m_CurrentIndex != dg.SelectedIndex)就失去了作用,不会响应。
然后考虑过使用MouseLeftButtonDown事件,但是这个事件居然只在表头才有作用...幸好随后又尝试了MouseLeftButtonUp事件,才顺利的实现了获取初始index的功能。
private void dg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
m_CurrentIndex = dg.SelectedI
另外,键盘上的方向键也不响应DataGrid的KeyDown事件,而KenUp事件则是响应的,不知道为什么...
2,数据交换后更新DataGrid,这个是跟CSDN的达人们学习的
dg.ItemsSource =dg.ItemsSource = tempS
先将数据源设置为null,然后重新设置数据源,OK,搞定
3,最后的空行不能交换,为实现这个功能,定义了两个数据列表
int itemsCount = 10;
for (int i = 0; i & itemsC i++)
source.Add(new Data()
FirstName = "First",
LastName = "Last",
Age = i.ToString(),
tempSource = UpDateTempSource(source);
dg.ItemsSource = tempS
其中的source用来用来执行数据交换,tempSource作为dg的数据源,最后添加了空行,确保最后一行不被交换,用
if (dg.SelectedIndex & source.Count - 1)进行判断。
完整代码:
MainPage.cs
public class Data
public string FirstName { }
public string LastName { }
public string Age { }
public partial class MainPage : UserControl
List&Data& source = new List&Data&();
List&Data& tempSource = new List&Data&();
DataGrid dataGrid = new DataGrid();
private int m_CurrentIndex = 0;
public MainPage()
InitializeComponent();
int itemsCount = 10;
for (int i = 0; i & itemsC i++)
source.Add(new Data()
FirstName = "First",
LastName = "Last",
Age = i.ToString(),
dg.ItemsSource = UpDateTempSource(source);
dg.FrozenColumnCount = 1;
dg.CellEditEnded+=new EventHandler&DataGridCellEditEndedEventArgs&(dg_CellEditEnded);
dg.MouseLeftButtonUp+=new MouseButtonEventHandler(dg_MouseLeftButtonUp);
dg.KeyUp+=new KeyEventHandler(dg_KeyUp);
private List&Data& UpDateTempSource(List&Data& source)
tempSource.Clear();
for (int i = 0; i & source.C i++)
tempSource.Add(source[i]);
tempSource.Add(new Data()
FirstName = "*",
LastName = "",
return tempS
private void dg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
m_CurrentIndex = dg.SelectedI
private void dg_KeyUp(object sender, KeyEventArgs e)
if (e.Key == Key.Up)
if (m_CurrentIndex != dg.SelectedIndex)
Data tem = source[m_CurrentIndex - 1];
source[m_CurrentIndex - 1] = source[m_CurrentIndex];
source[m_CurrentIndex] =
dg.ItemsSource =
dg.ItemsSource = UpDateTempSource(source);
m_CurrentIndex = m_CurrentIndex - 1;
dg.SelectedIndex = m_CurrentI
if (e.Key == Key.Down)
if (m_CurrentIndex != dg.SelectedIndex)
if (dg.SelectedIndex & source.Count - 1)
Data tem = source[m_CurrentIndex + 1];
source[m_CurrentIndex + 1] = source[m_CurrentIndex];
source[m_CurrentIndex] =
dg.ItemsSource =
dg.ItemsSource = UpDateTempSource(source);
m_CurrentIndex = m_CurrentIndex + 1;
dg.SelectedIndex = m_CurrentI
private void dg_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
if (dg.SelectedIndex == source.Count-1)
dg.ItemsSource =
source.Add(new Data()
FirstName = "*",
LastName = "",
dg.ItemsSource = UpDateTempSource(source);
private void btnDelete_Click(object sender, RoutedEventArgs e)
if (dg.SelectedIndex & 0)
source.RemoveAt(dg.SelectedIndex);
dg.ItemsSource =
dg.ItemsSource = UpDateTempSource(source);
private void btnInsert_Click(object sender, RoutedEventArgs e)
if (dg.SelectedIndex & 0)
source.Insert(dg.SelectedIndex, new Data() { FirstName = "F", LastName = "L", Age = (source.Count - 1).ToString() });
dg.ItemsSource =
dg.ItemsSource = UpDateTempSource(source);
为DataGridView控件动态添加新行并赋值
C#中/winform中,给已经绑定数据的datagridview后台动态添加新行
datagridview 手动添加控件列方法(图)
没有更多推荐了,private void toolLeft2_Click(object sender, EventArgs e)
int selcol = DBGrid2.CurrentCell.ColumnI
int selrow = DBGrid2.CurrentCell.RowI
DataGridViewColumn col = (DataGridViewColumn)DBGrid2.Columns[selcol].Clone();
DBGrid2.Columns.Insert(DBGrid2.CurrentCell.ColumnIndex - 1, col);
DBGrid2.Columns.RemoveAt(selcol + 1);
DBGrid2.CurrentCell = DBGrid2.Rows[selrow].Cells[selcol - 1];
private void toolRight2_Click(object sender, EventArgs e)
int selcol = DBGrid2.CurrentCell.ColumnI
int selrow = DBGrid2.CurrentCell.RowI
DataGridViewColumn col = (DataGridViewColumn)DBGrid2.Columns[selcol].Clone();
DBGrid2.Columns.Insert(DBGrid2.CurrentCell.ColumnIndex + 2, col);
DBGrid2.Columns.RemoveAt(selcol);
DBGrid2.CurrentCell = DBGrid2.Rows[selrow].Cells[selcol + 1];
private void DBGrid2_SelectionChanged(object sender, EventArgs e)
if (DBGrid2.CurrentCell == null)
if (DBGrid2.CurrentCell.ColumnIndex &= 1 || DBGrid2.CurrentCell.ColumnIndex &= DBGrid2.ColumnCount - 1)
toolLeft2.Enabled =
toolRight2.Enabled =
else if (DBGrid2.CurrentCell.ColumnIndex == 2)
toolLeft2.Enabled =
if (DBGrid2.CurrentCell.ColumnIndex == DBGrid2.ColumnCount - 2)
toolRight2.Enabled =
toolRight2.Enabled =
toolLeft2.Enabled =
if (DBGrid2.CurrentCell.ColumnIndex == DBGrid2.ColumnCount - 2)
toolRight2.Enabled =
toolRight2.Enabled =
C# 中DataGridView和ListView闪烁问题的解决方法
打印Datagridview中表格数据---c#.net
DataGridView取消默认选中行
C#打印DataGridView 中的数据
c# Datagridview控件实现指定字段查找功能【解决篇】
没有更多推荐了,示例程序是一个Windows窗体应用程序,有左右两个DataGridView控件:dgvLeft和dgvRight
dgvRight除时间外的每一行是dgvLeft的一列
private void Form1_Load(object sender, EventArgs e)
//C#中确定控件DataGridView根据内容自动调整列宽长度的属性
//是AutoSizeColumnsMode,将它的值设置为AllCells时可以达到该效果。
//调整行每行头部的宽度(可以更好地显示出数字)
this.dgvLeft.RowHeadersWidth = 50;
this.dgvRight.RowHeadersWidth = 50;
//左侧数据表
DataTable dtLeft = new DataTable();
dtLeft.Columns.Add("C1");
dtLeft.Columns.Add("C2");
dtLeft.Columns.Add("C3");
dtLeft.Columns.Add("TIME");
dtLeft.Rows.Add("1-1", "1-2", "1-3", DateTime.Now.ToShortTimeString());
dtLeft.Rows.Add("2-1", "2-2", "2-3", DateTime.Now.ToShortTimeString());
dtLeft.Rows.Add("3-1", "3-2", "3-3", DateTime.Now.ToShortTimeString());
dtLeft.Rows.Add("4-1", "4-2", "4-3", DateTime.Now.ToShortTimeString());
dtLeft.Rows.Add("5-1", "5-2", "5-3", DateTime.Now.ToShortTimeString());
dtLeft.Rows.Add("6-1", "6-2", "6-3", DateTime.Now.ToShortTimeString());
dgvLeft.DataSource = dtL
//右侧数据表,是左侧数据表的转置
DataTable dtRight = new DataTable();
for (int i = 0; i & dtLeft.Rows.C i++)
dtRight.Columns.Add("C" + i);
dtRight.Columns.Add("TIME");
for (int i = 0; i & dtLeft.Columns.Count - 1; i++)
object[] obj = new object[dtLeft.Rows.Count + 1];
for (int j = 0; j & dtLeft.Rows.C j++)
obj[j] = dtLeft.Rows[j][i];
obj[obj.Length - 1] = DateTime.Now.ToShortTimeString();
dtRight.Rows.Add(obj);
dgvRight.DataSource = dtR
每一行的行号可以通过下面的方法添加
//DataGridView 控件 dgvLeft 的 RowsAdded 事件
private void dgvLeft_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
for (int i = 0; i & dgvLeft.Rows.C i++)
this.dgvLeft.Rows[i].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.MiddleR
this.dgvLeft.Rows[i].HeaderCell.Value = i.ToString();
//DataGridView 控件 dgvLeft 的 RowsRemoved 事件
private void dgvLeft_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
for (int i = 0; i & dgvLeft.Rows.C i++)
this.dgvLeft.Rows[i].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.MiddleR
this.dgvLeft.Rows[i].HeaderCell.Value = i.ToString();
//DataGridView 控件 dgvRight 的 RowsAdded 事件
private void dgvRight_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
for (int i = 0; i & dgvRight.Rows.C i++)
this.dgvRight.Rows[i].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.MiddleR
this.dgvRight.Rows[i].HeaderCell.Value = i.ToString();
//DataGridView 控件 dgvRight 的 RowsRemoved 事件
private void dgvRight_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
for (int i = 0; i & dgvRight.Rows.C i++)
this.dgvRight.Rows[i].HeaderCell.Style.Alignment =
DataGridViewContentAlignment.MiddleR
this.dgvRight.Rows[i].HeaderCell.Value = i.ToString();
& 著作权归作者所有
人打赏支持
领取时间:
领取条件:开源项目被开源中国收录的开发者可领取
“”在线下联结了各位 OSCer,推广开源项目和理念,很荣幸有你的参与~
领取时间:
领取条件:参与过开源中国“源创会”的 OSCer 可以领取
码字总数 462457
评论删除后,数据将无法恢复
一、控件的一些行列设置 DataGridView dgv=//设计器中DataGridView名称为showdata dgv.AllowUserToAddRows =//不显示最后那个"*"行。 dgv.AutoSizeColumnsMode = DataGridVi......
在visual c#中ADO.NET 2.0中的新数据绑定技术介绍。 将bindingSource1绑定到数据,将TextBox控件绑定到bindingSource1。若要执行此操作,可将下面的代码粘贴到窗体中,并从窗体的构造函数调用...
一、写在前面 深复制需要将对象实例中字段引用的对象也进行复制,在平时的编程工作中经常要用到这种复制方式,因为很多时候我们复制一个对象实例A到实例B,在用实例B去做其他事情的时候,会对...
工作中遇到一个场景:在DataGridView上单击鼠标右键弹出快捷菜单,在快捷菜单内需要有“删除本行”、“清空数据”等按钮,于是我就自己实现了一个: 实现步骤如下: 步骤1,建立一个C#下的W...
一、DataGridView数据使用几大步骤(转载自http://zhidao.baidu.com/question/.html) (有一些小错误,已改正) 1、设置控件的外观; DataGridView dgv=new DataGridView(); dgv.B...
没有更多内容
加载失败,请刷新页面
一行代码实现增删改查、支持自定义结果集。 2个测试类(先看效果) public class UserDao extends Dao {public User login(User user) throws Exception{return (User) executeQueryO...
https://blog.csdn.net/liu_005/article/details/
沉迷于编程的小菜菜
&div class="layui-form-item magb0"&
&label class="layui-form-label"&富文本内容&/label&
&div class="layui-input-block"&
&textarea class="layui-tex......
if(window.indexedDB){ console.log('支持'); }else{ console.log('不支持'); } var students=[]; for(var i=0; i&20000; i++){ students.push({ id: i, name: 'wangwu' + i, age: 1+i, ema......
HTTP协议总览 在现今世界,我们的生活基本上已经来不开互联网,而HTTP协议基本就是互联网的基础。对HTTP协议有一个整体上的认识,不仅能帮我们更好的理解WEB系统,也能让我们在平时的开发中更...
jackie8tao
没有更多内容
加载失败,请刷新页面
文章删除后无法恢复,确定取消删除此文章吗?
亲,自荐的博客将通过私信方式通知管理员,优秀的博客文章审核通过后将在博客推荐列表中显示
确定推荐此文章吗?
确定推荐此博主吗?
聚合全网技术文章,根据你的阅读喜好进行个性推荐
指定官方社区
深圳市奥思网络科技有限公司版权所有

我要回帖

更多关于 交换顺序后意思不变的词语 的文章

 

随机推荐