c#的dgv列值无法按改订的值输出[碰到高手,已改] 不指定

Else , 2008/06/03 11:27 , 工蚁的忙碌工作 , Comments(0) , Reads(1418) , Via Original
[codes=C#] 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";

                DataGridView dv = dataGridView1;
              

                for (int i = 0; i < dv.RowCount; i++)
                {
                    for (int j = 0; j < dv.ColumnCount; j++)
                    {
                       // myfile += i.ToString() + "\t" + j.ToString() + "\t" + "\t";
                        label1.Text = i.ToString() + "\t" + j.ToString() + "\t" + "\t";
                        if (j + 1 < dv.ColumnCount)
                      {
                          myfile += dv[j, i].Value == null ? "null\t," : "\"" + dv[j, i].Value.ToString() + "\",";
                      }
                      else
                      {

                          myfile += dv[j, i].Value == null ? "null\t" : "\"" + dv[j, i].Value.ToString() + "\"";
                      }
                      
                      
                    
                    }
                    myfile += "\n";
                
                }




                    //write now
                    s_SW.WriteLine(myfile);
                s_SW.Close();
                txt_filename.Enabled = true;  

            }
                
              
            [/codes]

点击下面,查看已改的方法,谢谢csdn的朋友 白荷师傅

[codes=C#]      ///
        /// 把dgv寫到txt中
        ///

        ///
        ///
        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;
            }
        }[/codes]
Add a comment

Nickname

Site URI

Email

Enable HTML Enable UBB Enable Emots Hidden Remember [Login] [Register]