ASP.NET的DataGrid大量新增資料

來源:互聯網
上載者:User
ASP.NET的DataGrid大量新增資料
在以前的一個學校項目中,學生管理系統中應用了
ASP.NET的DataGrid大量新增資料,應用DataGrid的迴圈添加資料
for (i = 0; i < (int)this.MyDataGrid.Items.Count; i++)
{
//處理
}

瀏覽資料

添加資料

資料庫表資料
學生表
stu_Information
    [id] [nvarchar]
    [name] [nvarchar]
    [sex] [nvarchar]
    [birth] [nvarchar]
    [age] [nvarchar]
    [department] [nvarchar]
    [class] [nvarchar]
    [post] [nvarchar]
    [Political] [nvarchar]
    [address] [nvarchar]
    [email] [nvarchar]
    [phone] [nvarchar]
    [Telephone1] [nvarchar]
    [Telephone2] [nvarchar]
    [Information] [nvarchar]
考勤表
kaoqin
    [kaoqinID] [int] IDENTITY (1, 1) NOT NULL ,
    [datetime] [datetime] NULL ,
    [id] [char]
    [title] [char]
    [week] [char]
    [dayno] [char]
    [type] [char]
    [times] [int] NULL ,
    [information] [char]

頁面aspx檔案
<asp:DataGrid ID="MyDataGrid" AutoGenerateColumns="False" Width="100%" HeaderStyle-HorizontalAlign="center"
                ItemStyle-HorizontalAlign="center" runat="server">
                <Columns>
                    <asp:BoundColumn DataField="id" SortExpression="id" HeaderStyle-Width="10%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff" HeaderText="學號" />
                    <asp:BoundColumn DataField="name" SortExpression="name" HeaderStyle-Width="8%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff" HeaderText="姓名" />
                    <asp:BoundColumn DataField="sex" SortExpression="sex" HeaderStyle-Width="5%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff" HeaderText="性別" />
                    <asp:TemplateColumn HeaderText="遲到(次數)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff">
                        <ItemTemplate>
                            <asp:TextBox ID="Late" Width="30px" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="早退(次數)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff">
                        <ItemTemplate>
                            <asp:TextBox ID="LeaveEarly" Width="30px" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="曠課(次數)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff">
                        <ItemTemplate>
                            <asp:TextBox ID="Absenteeism" Width="30px" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="請假(次數)" HeaderStyle-Width="15%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff">
                        <ItemTemplate>
                            <asp:TextBox ID="Leave" Width="30px" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="備忘" HeaderStyle-Width="40%" HeaderStyle-CssClass="topbar"
                        HeaderStyle-ForeColor="#ffffff">
                        <ItemTemplate>
                            <asp:TextBox ID="Information" Width="200px" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
後台aspx.cs
protected void btn_Add(object sender, ImageClickEventArgs e)
    {
        string link1 = ConfigurationSettings.AppSettings["link_local"].ToString();
        SqlConnection conn1 = new SqlConnection(link1);
        string sqlcmd = "";
        int i;
        bool flag = false;
        conn1.Open();
        for (i = 0; i < (int)this.MyDataGrid.Items.Count; i++)
        {
            string typename = "";
            if (((TextBox)MyDataGrid.Items[i].Cells[3].Controls[1]).Text.Trim() != "")
            {
                typename = "遲到";
                flag = true;
                sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information)  values(@id,@week,@datetime,@type,@times,@information)";
                SqlCommand comm = new SqlCommand(sqlcmd, conn1);
                comm.Parameters.Add("@id", SqlDbType.Char, 20);
                comm.Parameters.Add("@week", SqlDbType.Char, 10);
                comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
                comm.Parameters.Add("@type", SqlDbType.Char, 10);
                comm.Parameters.Add("@times", SqlDbType.Char, 10);
                comm.Parameters.Add("@information", SqlDbType.Char, 100);

                comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
                comm.Parameters["@week"].Value = Week.SelectedItem.Value;
                comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
                comm.Parameters["@type"].Value = typename.Trim();
                comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[3].Controls[1]).Text.Trim();
                comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
                comm.ExecuteNonQuery();
            }
            if (((TextBox)MyDataGrid.Items[i].Cells[4].Controls[1]).Text.Trim() != "")
            {
                typename = "早退";
                flag = true;
                sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information)        values(@id,@week,@datetime,@type,@times,@information)";
                SqlCommand comm = new SqlCommand(sqlcmd, conn1);
                comm.Parameters.Add("@id", SqlDbType.Char, 20);
                comm.Parameters.Add("@week", SqlDbType.Char, 10);
                comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
                comm.Parameters.Add("@type", SqlDbType.Char, 10);
                comm.Parameters.Add("@times", SqlDbType.Char, 10);
                comm.Parameters.Add("@information", SqlDbType.Char, 100);

                comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
                comm.Parameters["@week"].Value = Week.SelectedItem.Value;
                comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
                comm.Parameters["@type"].Value = typename.Trim();
                comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[4].Controls[1]).Text.Trim();
                comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
                comm.ExecuteNonQuery();
            }
            if (((TextBox)MyDataGrid.Items[i].Cells[5].Controls[1]).Text.Trim() != "")
            {
                typename = "曠課";
                flag = true;
                sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information)        values(@id,@week,@datetime,@type,@times,@information)";
                SqlCommand comm = new SqlCommand(sqlcmd, conn1);
                comm.Parameters.Add("@id", SqlDbType.Char, 20);
                comm.Parameters.Add("@week", SqlDbType.Char, 10);
                comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
                comm.Parameters.Add("@type", SqlDbType.Char, 10);
                comm.Parameters.Add("@times", SqlDbType.Char, 10);
                comm.Parameters.Add("@information", SqlDbType.Char, 100);

                comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
                comm.Parameters["@week"].Value = Week.SelectedItem.Value;
                comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
                comm.Parameters["@type"].Value = typename.Trim();
                comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[5].Controls[1]).Text.Trim();
                comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
                comm.ExecuteNonQuery();
            }
            if (((TextBox)MyDataGrid.Items[i].Cells[6].Controls[1]).Text.Trim() != "")
            {
                typename = "請假";
                flag = true;
                sqlcmd = "insert into kaoqin(id,week,datetime,type,times,information)        values(@id,@week,@datetime,@type,@times,@information)";
                SqlCommand comm = new SqlCommand(sqlcmd, conn1);
                comm.Parameters.Add("@id", SqlDbType.Char, 20);
                comm.Parameters.Add("@week", SqlDbType.Char, 10);
                comm.Parameters.Add("@datetime", SqlDbType.DateTime, 8);
                comm.Parameters.Add("@type", SqlDbType.Char, 10);
                comm.Parameters.Add("@times", SqlDbType.Char, 10);
                comm.Parameters.Add("@information", SqlDbType.Char, 100);

                comm.Parameters["@id"].Value = this.MyDataGrid.Items[i].Cells[0].Text.Trim();
                comm.Parameters["@week"].Value = Week.SelectedItem.Value;
                comm.Parameters["@datetime"].Value = DateTime.Now.ToString("yyyy年MM月dd日");
                comm.Parameters["@type"].Value = typename.Trim();
                comm.Parameters["@times"].Value = ((TextBox)MyDataGrid.Items[i].Cells[6].Controls[1]).Text.Trim();
                comm.Parameters["@information"].Value = ((TextBox)this.MyDataGrid.Items[i].Cells[7].Controls[1]).Text.Trim();
                comm.ExecuteNonQuery();
            }
        }
        conn1.Close();
        if (flag == true)
        {
            lblMessage.Text = "添加成功!";
        }
        else
            lblMessage.Text = "未添加資訊";
    }
原始碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.