Csharp windowform datagridview Clipboard TO EXCEL OR FROM EXCEL DATA 保存datagridview所有數據

from http://www.codeproject.com/KB/office/DataGridViewCopyPaste.aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/// <summary>
/// 分行顯示顏色
/// 塗聚文 2011-10-13
/// 締友計算機信息技術有限公司
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    //分行顯示顏色
    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
        if (i % 2 == 0)
        {
            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightPink;
        }
    }
}
/// <summary>
/// 保存dataGridView所有記錄
/// 塗聚文 2011-10-13
/// 締友計算機信息技術有限公司
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bindingNavigatoSaveNewItem_Click(object sender, EventArgs e)
{
    int cum = 0;
    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
        administrativeAreaListInfo.AreaCode = dataGridView1[0, i].Value.ToString();
        administrativeAreaListInfo.AreaName = dataGridView1[1, i].Value.ToString();
        administrativeAreaListInfo.AreaParentID = int.Parse(dataGridView1[2, i].Value.ToString());
        int k = administrativeAreaListBLL.InsertAdministrativeAreaList(administrativeAreaListInfo);
        if (k >= 1)
        {
            //MessageBox.Show("oK");
            //this.DialogResult = DialogResult.OK;
            cum = cum + 1;
        }
        else
        {
            this.DialogResult = DialogResult.Cancel;
 
        }
    }
    DialogResult dresult = MessageBox.Show("添加:" + cum.ToString() + "條記錄!","信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    if (dresult == DialogResult.OK)
    {
        this.Close();
        this.DialogResult = DialogResult.OK;
    }
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    // 设定单元格的默认值
    e.Row.Cells["AreaParentID"].Value = AreaParentID;
    e.Row.Cells["AreaParentName"].Value = AreaParentName;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    if ((e.Control && e.KeyCode == Keys.Delete) || (e.Shift && e.KeyCode == Keys.Delete))
    {
        CopyClipboard();
    }
    if ((e.Control && e.KeyCode == Keys.Insert) || (e.Shift && e.KeyCode == Keys.Insert))
    {
        PasteClipboard();
    }
    
}
/// <summary>
/// 復制
/// </summary>
private void CopyClipboard()
{
    DataObject d = dataGridView1.GetClipboardContent();
    Clipboard.SetDataObject(d);
}
 
/// <summary>
/// 粘貼
/// </summary>
private void PasteClipboard()
{
    try
    {
        string s = Clipboard.GetText();
        string[] lines = s.Split('\n');
        int iFail = 0, iRow = dataGridView1.CurrentCell.RowIndex;
        int iCol = dataGridView1.CurrentCell.ColumnIndex;
        DataGridViewCell oCell;
        foreach (string line in lines)
        {
            if (iRow < dataGridView1.RowCount && line.Length > 0)
            {
                string[] sCells = line.Split('\t');
                for (int i = 0; i < sCells.GetLength(0); ++i)
                {
                    if (iCol + i < this.dataGridView1.ColumnCount)
                    {
                        oCell = dataGridView1[iCol + i, iRow];
                        if (!oCell.ReadOnly)
                        {
                            if (oCell.Value.ToString() != sCells[i])
                            {
                                oCell.Value = Convert.ChangeType(sCells[i], oCell.ValueType);
                                oCell.Style.BackColor = Color.Tomato;
                            }
                            else
                                iFail++;//
                        }
                    }
                    else
                    { break; }
                }
                iRow++;
            }
            else
            { break; }
            if (iFail > 0)
                MessageBox.Show(string.Format("{0} 更新失敗,因為只讀設置列", iFail));
        }
    }
    catch (FormatException)
    {
        MessageBox.Show("您粘貼錯誤的數據格式為單元格");
        return;
    }
}
/// <summary>
/// 復制到EXCEL
/// 塗聚文 2011-10-13
/// 締友計算機信息技術有限公司
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    CopyClipboard();
}
/// <summary>
/// 從EXCEL粘貼
/// 塗聚文 2011-10-13
/// 締友計算機信息技術有限公司
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pasteCtrlVToolStripMenuItem_Click(object sender, EventArgs e)
{
    PasteClipboard();
}
posted @   ®Geovin Du Dream Park™  阅读(752)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2011年10月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示