Scenario :- I have to perform CRUD operation for selected records ( check boxes) in a GridView ID as described in the image.
No what we are doing is to perform a for loop on datatgrid & get selected checkbox id in a string varibale separted by ','.After that we will create a DB function that will split the checkbox ID'd string in new row for each id.
It will make CRUD operation very easy
GridView in aspx:-
<asp:DataGrid ID="grdList" DataKeyField="diagnosis_id" Runat="server" AutoGenerateColumns="False"
AllowPaging="False" PageSize="100" ShowHeader="true" >
<Columns>
<asp:BoundColumn DataField="diagnosis_name" HeaderText="DIAGNOSIS" SortExpression="diagnosis_name asc">
<HeaderStyle Width="75%" HorizontalAlign="Center" ></HeaderStyle>
<ItemStyle CssClass="greylabelsmall"></ItemStyle>
</asp:BoundColumn>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
VB function to get selected checkboxes in datagrid
Private Sub GetCheckedBox()
For Each item As DataGridItem In grdICDList.Items
If CType(item.FindControl("chkICD"), CheckBox).Checked Then
icdIds = icdIds + Convert.ToString( grdList .DataKeys.Item(item.ItemIndex)) + ","
End If
Next
If icdIds.Length > 0 Then
If icdIds.LastIndexOf(",") > 0 Then
icdIds = icdIds.Substring(0, icdIds.Length - 1)
End If
'SaveICDs()
InsertDiagnosis(icdIds)
End If
End Sub
Now create a db Function like this:-
CREATE FUNCTION [dbo].[Split]
(
@RowData varchar(8000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
And Use it in your stored procedures like this :-
INSERT INTO INSERTABC
(CPTID,
ICDID,
CREATED_USER,
CREATED_DATE,
ISDELETED)
SELECT @CPTID,LTRIM(RTRIM(DATA)), @CREATED_USER, GETDATE(),0 FROM SPLIT(@ICDIDS,','
Example :--
SELECT DATA FROM SPLIT(@ICDIDS, ',' )
Data will give you select check boxes ID in new rows.
No comments:
Post a Comment