Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to select multiple checkbox in gridview and stored those selected checkbox value into databse...

my design page is:

XML
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        Height="298px" Width="593px"
        onselectedindexchanged="GridView1_SelectedIndexChanged"
        onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField HeaderText="ID" DataField="ID" />
            <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
            <asp:BoundField HeaderText="Price" DataField="price" />
            <asp:BoundField HeaderText="Category" DataField="category" />
            <asp:TemplateField HeaderText="Product">
             <ItemTemplate>
             <asp:Image width="40" Height="40" ImageUrl='<%#Eval("path")%>' runat ="server"  ID="image1" />
             </ItemTemplate>
             </asp:TemplateField>
  <asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
        </Columns>
    </asp:GridView>
</asp:Content>


my c# coding is:
C#
protected void Button1_Click(object sender, EventArgs e)
   {



for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb =(CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");//Gets the


if (cb.Checked == true)
{
    
   // how to get those select values and how store those values in database
}
else
{ //Do something here when CheckBox is UnChecked }
}

}
}
Posted
Updated 11-Feb-13 19:20pm
v2
Comments
Nandakishore G N 11-Feb-13 23:48pm    
what have done till now? paste it

1 solution

Store your selected checkbox value in string:-
I am using Telerik RadGrid, you can modify it by your GridView.
C#
  string strMappedUser = "";
  foreach (GridDataItem gdt in gridUsers.MasterTableView.Items)
  {
      CheckBox chb = (CheckBox)gdt.FindControl("cbSelectUser");
      if (chb.Checked == true)
      {
        if (strMappedUser == "")
           strMappedUser = Convert.ToString(gdt.GetDataKeyValue("UserId"));
        else
           strMappedUser = strMappedUser + "," + Convert.ToString(gdt.GetDataKeyValue("UserId"));
     }
}


Now in Database Use this :-
SQL
CREATE PROCEDURE Sp_Example (
	@aintUserId VARCHAR(500) = ''
	)
AS
BEGIN
	CREATE TABLE #tempTable (
		Id INT
		,UsID VARCHAR(50)
		)

	INSERT INTO #tempTable (
		id
		,UsID
		)
	SELECT Row AS ID
		,Value AS SetValue
	FROM dbo.fn_SplitForOrdering(@aintUserId, ',')

	INSERT INTO t_YourTable (UserId)
	SELECT UsID
	FROM #tempTable

	DROP TABLE #tempTable
END


here is the function used for returning a varchar rowset from a delimited string

SQL
 CREATE FUNCTION dbo.fn_SplitForOrdering (
	@InputText VARCHAR(4000)
	,@Delimiter VARCHAR(10)
	)
RETURNS @Array TABLE (
	Row INT
	,Value VARCHAR(4000)
	)
AS
-----------------------------------------------------------  
-- returns a varchar rowset from a delimited string --  
-----------------------------------------------------------  
BEGIN
	DECLARE @Pos INT
	DECLARE @End INT
	DECLARE @TextLength INT
	DECLARE @DelimLength INT
	DECLARE @Row INT

	SET @TextLength = DataLength(@InputText)

	-- Exit function if no text is passed in  
	IF @TextLength = 0
		RETURN

	SET @Pos = 1
	SET @DelimLength = DataLength(@Delimiter)
	SET @Row = 1

	IF @DelimLength = 0
	BEGIN
		WHILE @Pos <= @TextLength
		BEGIN
			INSERT @Array (
				Row
				,Value
				)
			VALUES (
				@Row
				,SubString(@InputText, @Pos, 1)
				)

			SET @Pos = @Pos + 1
			SET @Row = @Row + 1
		END
	END
	ELSE
	BEGIN
		SET @InputText = @InputText + @Delimiter
		SET @End = CharIndex(@Delimiter, @InputText)

		WHILE @End > 0
		BEGIN
			INSERT @Array (
				Row
				,Value
				)
			VALUES (
				@Row
				,SubString(@InputText, @Pos, @End - @Pos)
				)

			SET @Pos = @End + @DelimLength
			SET @End = CharIndex(@Delimiter, @InputText, @Pos)
			SET @Row = @Row + 1
		END
	END

	RETURN
END


Hope you will get it.
Good luck.
 
Share this answer
 
v6
Comments
Deenuji 12-Feb-13 1:29am    
i dont know to use store PROCEDURE...i need w/o store PROCEDURE coding...pls
Raje_ 12-Feb-13 1:39am    
Then you must learn, how to use Stored Procedures.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900