原来123456列移动后
变成213456
这里的1 2 3 4 5 6 这个是列的
格的值 比如
111 222 333 444 555 666
在换他们顺序的时候
变成(可视)
222 1111 333 444 555 666
但我把这些移动后生成的值输入到文本上后发现,还是
123456这样的顺序可是在输出的文本里面是
111 222 333 444 555 666 谁能帮我Orz
变成213456
这里的1 2 3 4 5 6 这个是列的
格的值 比如
111 222 333 444 555 666
在换他们顺序的时候
变成(可视)
222 1111 333 444 555 666
但我把这些移动后生成的值输入到文本上后发现,还是
123456这样的顺序可是在输出的文本里面是
111 222 333 444 555 666 谁能帮我Orz
/// 把dgv寫到txt中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_2txt_Click(object sender, EventArgs e)
{ if (txt_filename.Text.Trim() == "")
{
lb_showmsg.Text = "文件名為空不能創建"; }
else
{
txt_filename.Enabled = false;
lb_showmsg.Text = "文件名:" + txt_filename.Text + ".txt 創建成功"; StreamWriter s_SW = new StreamWriter(@"D:\" + txt_filename.Text + ".txt", true); string myfile = "\n";
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j<dataGridView1.ColumnCount; j++)
{
// myfile += i.ToString() + "\t" + j.ToString() + "\t" + "\t";
label1.Text = i.ToString() + "\t" + j.ToString() + "\t" + "\t";
if (j+1<dataGridView1.ColumnCount)
{
myfile += dataGridView1[j,i].Value==null? "null\t,":"\""+dataGridView1[j, i].Value.ToString()+"\",";
}
else
{
myfile += dataGridView1[j,i].Value==null? "null\t":"\""+dataGridView1[j, i].Value.ToString()+"\"";
}
}
myfile += "\n";
}
//write now
s_SW.WriteLine(myfile);
s_SW.Close();
txt_filename.Enabled = true; }
}
使用GridX...
Developer Express .NET v7.2
{ //向左
try
{
this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].DisplayIndex = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].DisplayIndex - 1;
// y++;
}
catch
{
this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].DisplayIndex = 0;
//y = 0;
}
} private void btn_prv_Click(object sender, EventArgs e)
{ //向右
try
{
this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].DisplayIndex = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].DisplayIndex + 1;
// y++;
}
catch
{
this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].DisplayIndex = this.dataGridView1.ColumnCount - 1;
//y = 0;
}
}
private int GetDisplayIndex(int index)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (dataGridView1.Columns[i].DisplayIndex == index)
{
return i;
}
}
}
而你的代码里这样修改:for (int j = 0; j<dataGridView1.ColumnCount; j++)
{
// myfile += i.ToString() + "\t" + j.ToString() + "\t" + "\t";
label1.Text = i.ToString() + "\t" + j.ToString() + "\t" + "\t";
int k=GetDisplayIndex(j);//取得真正输出的列
if (j+1<dataGridView1.ColumnCount)
{
myfile += dataGridView1[k,i].Value==null? "null\t,":"\""+dataGridView1[k, i].Value.ToString()+"\",";
}
else
{
myfile += dataGridView1[k,i].Value==null? "null\t":"\""+dataGridView1[k, i].Value.ToString()+"\"";
}
}
private void btn_2txt_Click(object sender, EventArgs e)
{
int[] index = new int[this.dataGridView1.Columns.Count];
int i = 0;
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
index[i] = column.DisplayIndex;
i++;
} if (txt_filename.Text.Trim() == "")
{
lb_showmsg.Text = "文件名為空不能創建"; }
else
{
txt_filename.Enabled = false;
lb_showmsg.Text = "文件名:" + txt_filename.Text + ".txt 創建成功"; StreamWriter s_SW = new StreamWriter(txt_filename.Text + ".txt", true);
string myfile = "\n";
for (i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
// myfile += i.ToString() + "\t" + j.ToString() + "\t" + "\t";
label1.Text = i.ToString() + "\t" + j.ToString() + "\t" + "\t";
if (j + 1 < dataGridView1.ColumnCount)
{
myfile += dataGridView1[index[j], i].Value == null ? "null\t," : "\"" + dataGridView1[index[j], i].Value.ToString() + "\",";
}
else
{ myfile += dataGridView1[index[j], i].Value == null ? "null\t" : "\"" + dataGridView1[index[j], i].Value.ToString() + "\"";
}
}
myfile += "\n"; }
//write now
s_SW.WriteLine(myfile);
s_SW.Close();
txt_filename.Enabled = true;
}
}
首先将列的索引保存起来:int[] index = new int[this.dataGridView1.Columns.Count];
int i = 0;
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
index[i] = column.DisplayIndex;
i++;
}然后循环的时候查找索引:
myfile += dataGridView1[index[j], i].Value == null ? "null\t," : "\"" + dataGridView1[index[j],i].Value.ToString() + "\",";