如何获取网格视图复选框选中行数据到数据库中 [英] how to get grid view check box checked rows data into database

查看:94
本文介绍了如何获取网格视图复选框选中行数据到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图。在这个我有一个复选框(在每一行)。还有一个文本框。

现在我想插入文本框和一些列的数据(例如''产品类型'')点击网格视图外的按钮进入我的数据库....

我怎么能这样做也告诉我存储过程的方式来做这个....



给一些建议的朋友......

解决方案

参考这篇文章 -



http:// www .aspsnippets.com / Articles / Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx [ ^ ]


此存储过程可能是:

  CREATE   PROCEDURE  [ProcedureName] 
- 添加此处存储过程的参数
@ Parameter1 INT
AS
BEGIN
SET NOCOUNT ON ;

BEGIN TRANSACTION

- 如果表alredy存在于表update中,则else插入新行
IF EXISTS SELECT *
FROM [TableName]
WHERE < conditions>)
BEGIN
更新 [TableName]
SET [FieldName] = [值]
WHERE < conditions>
END
ELSE
BEGIN
INSERT INTO < tablename> VALUES < ............>
END


IF @@ ROWCOUNT = 0
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
END
END





点击按钮事件按钮执行此操作:

  protected   void  btnSave_Click(对象发​​件人,EventArgs e)
{
SqlConnection conn =< your connectionstring = > ;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = conn;

字符串 sqlQuery = ;

尝试
{
conn.Open();

for int i = 0 ; i < yourdgv.RowCount - 1 ; i ++)
{
sqlCommand.CommandText = < yourprocedurename>;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand .Parameters.AddWithValue( @ parameter1,< value>);

sqlCommand.ExecuteNonQuery();

// 数据插入已完成。
}
}
catch (例外)
{
// < span class =code-comment>你的例外......

}
最后
{
conn.Close();
}
}





...

Plz。如果有帮助,请告诉我。


使用以下代码...

  protected   void  btnSaveData_Click( object  sender,EventArgs e)
{
foreach (GridViewRow row in grvYourGrid.Rows)
{
CheckBox chk = (CheckBox)row.FindControl( YourCheckBoxID);
if (chk.Checked)
{
string textInTextBox = ((TextBox)row.FindControl( YourTextBoxID))。文本;
// 与其他控件相同
// 在此执行数据库操作
}
}
}


i have a grid view. and in this i have a check box (in each row).and a text box also.
now i want to insert data of text box and some column(for example ''product type'') into my database on click of a button that is outside grid view....
how can i do this also tell me stored procedure way to do this....

give some suggetion friends...

解决方案

refer this Article-

http://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx[^]


The stored procedure for this could be:

CREATE PROCEDURE [ProcedureName] 
	-- Add the parameters for the stored procedure here
	@Parameter1 INT
AS
BEGIN
    SET NOCOUNT ON;
	
    BEGIN TRANSACTION
    	
	-- if row alredy exists in a table update it else insert new row
    IF EXISTS ( SELECT  *
                FROM    [TableName]
                WHERE   <conditions> )
        BEGIN
            UPDATE  [TableName]
            SET     [FieldName] = [Value]
            WHERE   <conditions>
        END
    ELSE 
        BEGIN
	    INSERT INTO <tablename> VALUES <............>
        END
        
        
	 IF @@ROWCOUNT = 0 
        BEGIN
            ROLLBACK TRANSACTION
        END
    ELSE 
        BEGIN
            COMMIT TRANSACTION
        END
END



And in Click Event of a button do this:

protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = <your connectionstring="">;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;

            String sqlQuery = "";

            try
            {
               conn.Open();

               for (int i = 0; i < yourdgv.RowCount - 1; i++)
               {
                 sqlCommand.CommandText = "<yourprocedurename>";
                 sqlCommand.CommandType = CommandType.StoredProcedure;
                 sqlCommand .Parameters.AddWithValue("@parameter1", <value>);

                 sqlCommand.ExecuteNonQuery();

                 //Data insertion completed.
               }
            }
            catch (Exception)
            {
                // your exception..
            }
            finally
            {
                conn.Close();
            }
        }



...
Plz. Let me know if this helps.


use following code...

protected void btnSaveData_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in grvYourGrid.Rows)
    {
        CheckBox chk = (CheckBox)row.FindControl("YourCheckBoxID");
        if (chk.Checked)
        {
            string textInTextBox =((TextBox)row.FindControl("YourTextBoxID")).Text;
            //same like for other controls
            //perform your database operation here
        }
    }
}


这篇关于如何获取网格视图复选框选中行数据到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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