2009-11-05 65 views

回答

2

是的,你可以!

看看一些代碼here,看看是否有幫助。

0

您可以讀取excel工作表到數據集,然後將數據集綁定到網格。我認爲你應該能夠做出修改,然後在完成編輯時再次寫出excel。

3

這是從codeprojet writeup編輯的。稍有不同的`

private void PasteClipboard() 
     { 
      try 
      { 
       string s = Clipboard.GetText(); 
       string[] lines = s.Split('\n'); 
       int iFail = 0, iRow = dgData.CurrentCell.RowIndex; 
       int iCol = dgData.CurrentCell.ColumnIndex; 
       DataGridViewCell oCell; 
       if (dgData.Rows.Count < lines.Length) 
        dgData.Rows.Add(lines.Length-1); 
       foreach (string line in lines) 
       { 
        if (iRow < dgData.RowCount && line.Length > 0) 
        { 
         string[] sCells = line.Split('\t'); 
         for (int i = 0; i < sCells.GetLength(0); ++i) 
         { 
          if (iCol + i < this.dgData.ColumnCount) 
          { 
           oCell = dgData[iCol + i, iRow]; 
           if (!oCell.ReadOnly) 
           { 
            if (oCell.Value==null|| oCell.Value.ToString() != sCells[i]) 
            { 
             oCell.Value = Convert.ChangeType(sCells[i], 
                   oCell.ValueType); 
             // oCell.Style.BackColor = Color.Tomato; 
            } 
            else 
             iFail++; 
            //only traps a fail if the data has changed 
            //and you are pasting into a read only cell 
           } 
          } 
          else 
          { break; } 
         } 
         iRow++; 
        } 
        else 
        { break; } 
        if (iFail > 0) 
         MessageBox.Show(string.Format("{0} updates failed due" + 
             " to read only column setting", iFail)); 
       } 
      } 
      catch (FormatException) 
      { 
       MessageBox.Show("The data you pasted is in the wrong format for the cell"); 
       return; 
      } 
     } 
相關問題