如何通过ado.net中的datagridview将记录插入数据库? [英] how do i insert a record to database through datagridview in ado.net?

查看:77
本文介绍了如何通过ado.net中的datagridview将记录插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:叹息:我如何通过ado.net中的datagridview将记录插入数据库?

:sigh: how do i insert a record to database through datagridview in ado.net?

推荐答案

datagridview是表示层的一部分.它与插入记录无关.该控件可能支持某种黑客手段,但您不应使用它.编写您自己的代码以插入记录,然后重新绑定到控件以显示已更改的数据.
A datagridview is part of your presentation layer. It has nothing to do with inserting records. The control may support some sort of hack, but you should not use it. Write your own code to insert records, then rebind to your control to show the data that has changed.


我正在考虑将您的GridView绑定到DataSet .这是什么问题?
用新记录更新DataSet / DataTable并调用存储过程以用新记录更新数据库.

如果要使用GridView插入行,请此处 [
I am considering your GridView is bound to a DataSet or a DataTable. What''s the problem with this?
Update the DataSet / DataTable with the new record and call a stored procedure to Update the database with new records.

If inserting rows using GridView is what you are looking for, have a look Here[^]


这取决于您要如何将记录插入数据库中,方法是单击gridview外部的多个按钮行插入或一次只插入一行...


通过单击gridview外部的按钮,她是多行插入的代码.




html代码


it depends how you want to insert the records into database by clicking the button outside gridview for multiple rows insert or just to insert one row at a time...


her is the code for multiple rows insert by clicking the button outside gridview.




html code


<asp:GridView ID="gvpostslambook" runat="server" AutoGenerateColumns="False" BackColor="White"
                    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
                    EmptyDataText="This person has not selected any questions yet">
                    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="#DCDCDC" />
                    <EmptyDataRowStyle BackColor="#0033CC" Font-Bold="True" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <table>
                                    <tr>
                                        <td style="width: 300px; height: 50px">
                                            <asp:Label Font-Bold="true" ForeColor="DarkBlue" ID="Label1" runat="server" Text=''<%#Eval("question_desc") %>''></asp:Label>
                                        </td>
                                        <td style="width: 10px">
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txt" TextMode="MultiLine" runat="server" Height="70px" Width="300px"></asp:TextBox>
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="lbtnargument" Visible="false" runat="server" CommandArgument=''<%#Eval("question_id") %>''></asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>




使用gridview内的链接按钮获取每个行的命令参数



单击您要为其插入记录的按钮时






Use a linkbutton inside gridview to get commandargument for eachrow



on clicking the button for which you want to insert the record



foreach(gridviewrow row in gridviewid.rows)
{
// to insert the value inside textbox to database, find each textbox 
// control

linkbutton lbtn=(linkbutton)row.findcontrol("linkbuttonid");
textbox txt=(textbox)row.findcontrol("textboxid");
//now get the commandargument from linkbutton

//if you have used the properties then get the value inside properties

property.userid=convert.toint32(lbtn.commandargument);
property.name=txt.text.trim();
// now insert these values in database..
//it will help you to insert mutilple records in database
}


这篇关于如何通过ado.net中的datagridview将记录插入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆