Click here to Skip to main content
11,491,742 members (67,102 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
hello sir/mam
i want to add checkbox feild at run time in datagridview. iwant to display datatable value with checkbox in grid view. my code is
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim rst1, rst2, rst3, dt As New DataTable
Dim cm As SqlCommand
Dim cmbd As SqlCommandBuilder
Dim rs1 As MsgBoxResult
Dim dr, dr1 As DataRow
Dim ip, url1 As String
Dim i, c, q, j, trans, p, rowcnt As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New SqlConnection("Data Source=.;Initial Catalog=appraisal;User ID=sa;Password=database")
con.Open()
getques()
End Sub
Public Sub getques()
cm = New SqlCommand("SELECT o.option_id,q.ques_id, q.ques_txt, o.option_txt FROM ques_mst q INNER jOIN option_mst o ON q.ques_id = o.ques_id ", con)
da = New SqlDataAdapter(cm)
cmbd = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "q1")
dt = New DataTable
dt = ds.Tables("q1")
dg_ques.DataSource = ds.Tables("q1")
dg_ques.DataBind()
rst3 = New DataTable
rst3.Columns.Add("quesid")
rst3.Columns.Add("questxt")
rst3.Columns.Add("op1")
rst3.Columns.Add("opt2")
rst3.Columns.Add("opt3")
rst3.Columns.Add("opt4")
rst3.Columns.Add("opt5")


cm = New SqlCommand("select * from ques_mst order by queS_id", con)
da = New SqlDataAdapter(cm)
ds = New DataSet
da.Fill(ds, "ques")
rst1 = New DataTable
rst1 = ds.Tables("ques")
If rst1.Rows.Count > 0 Then
i = rst1.Rows.Count
c = 0
While c < i
cm = New SqlCommand("select * from option_mst where queS_id = '" & rst1.Rows(c).Item("ques_id") & "'", con)
da = New SqlDataAdapter(cm)
ds = New DataSet
da.Fill(ds, "opt")
rst2 = New DataTable
rst2 = ds.Tables("opt")
If rst2.Rows.Count > 0 Then
p = rst2.Rows.Count
q = 0
rowcnt = rst3.Rows.Count
rst3.Rows.Add()
rst3.Rows(rowcnt).Item(0) = rst1.Rows(c).Item(0).ToString
rst3.Rows(rowcnt).Item(1) = rst1.Rows(c).Item(1).ToString
While q < p
rst3.Rows(rowcnt).Item(q + 2) = rst2.Rows(q).Item(2).ToString
q = q + 1
End While
End If
c = c + 1
End While
End If
dt = New DataTable
dt = rst3
c = 0
j = 0
While c <= rst3.Rows.Count - 1
Me.dg_ques.Rows(c).Cells(0).Text = rst3.Rows(c).Item(0).ToString
Me.dg_ques.Rows(c).Cells(1).Text = rst3.Rows(c).Item(1).ToString
p = rst3.Columns.Count
q = 2
While q < p - 1
Me.dg_ques.Rows(c).Cells(q).Text = rst3.Rows(c).Item(q).ToString
q = q + 1
End While
c = c + 1
End While



con.Close()

End Sub
Protected Sub grid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = DirectCast(e.Row.FindControl("checkBoxID"), CheckBox)
End If





End Sub




Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
End Class
here four feild option1,2,3,4 these are fetching the data from database in grid view.
i want that when i run this page then with these option there will be check box are also dispay with each option .
plz help me

this will show like
Posted 22-Feb-13 21:20pm
Edited 24-Feb-13 18:36pm
v2
Comments
willington.d at 25-Feb-13 4:18am
   
Couldn't understand. Do u want to display checkbox if the field contains 1,2,3,4?
supriya931 at 25-Feb-13 4:33am
   
no i want that checkbox will be display with the option value in the grid view
willington.d at 25-Feb-13 4:46am
   
Hi

You mean, You want to display Checkbox with value 1, 2, 3, 4 in all rows of Grid? or the checkbox will be changed by any condition?
supriya931 at 25-Feb-13 5:02am
   
no see the format is like

qid qtxt op1 op2 op3 op4
1 hghg hh then checkbox hg then checkbox gjg then checkbox hhg then checkbox
willington.d at 25-Feb-13 5:11am
   
Ok.. I understood now. So you have to dispaly Checkboxes in a grid based on the answers.
for example:
qid qtxt op1 op2 op3 op4
1 hghg hh then checkbox hg then checkbox gjg then checkbox hhg then checkbox
2 ere ChkBox chkbox
3 2325 chkbox chkbox chkbox

So u want like the above correct?
supriya931 at 25-Feb-13 5:18am
   
no no see
qid qtxt op1 op2 op3 op4

1 are you ready yes&chkbox no&chkbox notyet&chkbox 5min&chkbox

like this
willington.d at 25-Feb-13 4:58am
   
Hi Supriya

Please rfer to the below link:

http://www.c-sharpcorner.com/UploadFile/sushmita_kumari/dynamiccheckbox101112006231058PM/dynamiccheckbox1.aspx

http://stackoverflow.com/questions/5248568/asp-net-dynamic-checkbox

Regards
Willington
supriya931 at 25-Feb-13 5:05am
   
and another problem is that when i add checkbox in itemtemplate of the gridview then when i was run the application then it shows 5 time gerater rows form my database table with blank values
supriya931 at 25-Feb-13 5:08am
   
i sent to my code again

Partial Class _Default
Inherits System.Web.UI.Page
Dim con As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim rst1, rst2, rst3, dt As New DataTable
Dim cm As SqlCommand
Dim cmbd As SqlCommandBuilder
Dim rs1 As MsgBoxResult
Dim dr, dr1 As DataRow
Dim ip, cr, b1, b2, s1 As String
Dim i, c, q, j, trans, p, rowcnt As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New SqlConnection("Data Source=.;Initial Catalog=appraisal;User ID=sa;Password=database")
con.Open()
getques()
getsub()
'getacdmic_detail()
End Sub
Public Sub getques()
cm = New SqlCommand("SELECT o.option_id,q.ques_id, q.ques_txt, o.option_txt FROM ques_mst q INNER jOIN option_mst o ON q.ques_id = o.ques_id order by q.ques_id ", con)
da = New SqlDataAdapter(cm)
cmbd = New SqlCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "q1")
dt = New DataTable
dt = ds.Tables("q1")
dg_ques.DataSource = ds.Tables("q1")
dg_ques.DataBind()
rst3 = New DataTable
rst3.Columns.Add("quesid")
rst3.Columns.Add("questxt")
rst3.Columns.Add("op1")
rst3.Columns.Add("opt2")
rst3.Columns.Add("opt3")
rst3.Columns.Add("opt4")
cm = New SqlCommand("select * from ques_mst order by queS_id", con)
da = New SqlDataAdapter(cm)
ds = New DataSet
da.Fill(ds, "ques")
rst1 = New DataTable
rst1 = ds.Tables("ques")
If rst1.Rows.Count > 0 Then
i = rst1.Rows.Count
c = 0
While c < i
cm = New SqlCommand("select * from option_mst where queS_id = '" & rst1.Rows(c).Item("ques_id") & "'", con)
da = New SqlDataAdapter(cm)
ds = New DataSet
da.Fill(ds, "opt")
rst2 = New DataTable
rst2 = ds.Tables("opt")
If rst2.Rows.Count > 0 Then
p = rst2.Rows.Count
q = 0
rowcnt = rst3.Rows.Count
rst3.Rows.Add()
rst3.Rows(rowcnt).Item(0) = rst1.Rows(c).Item(0).ToString
rst3.Rows(rowcnt).Item(1) = rst1.Rows(c).Item(1).ToString
While q < p
rst3.Rows(rowcnt).Item(q + 2) = rst2.Rows(q).Item(2).ToString
q = q + 1
End While
End If
c = c + 1
End While
End If
dt = New DataTable
dt = rst3
c = 0
j = 0
While c <= dt.Rows.Count - 1
Me.dg_ques.Rows(c).Cells(0).Text = rst3.Rows(c).Item("quesid").ToString()
Me.dg_ques.Rows(c).Cells(1).Text = rst3.Rows(c).Item("questxt").ToString
Me.dg_ques.Rows(c).Cells(2).Text = rst3.Rows(c).Item("op1").ToString
Me.dg_ques.Rows(c).Cells(4).Text = rst3.Rows(c).Item("opt2").ToString
Me.dg_ques.Rows(c).Cells(6).Text = rst3.Rows(c).Item("opt3").ToString
Me.dg_ques.Rows(c).Cells(8).Text = rst3.Rows(c).Item("opt4").ToString
c = c + 1
End While

'While c <= rst3.Rows.Count - 1
' Me.dg_ques.Rows(c).Cells(0).Text = rst3.Rows(c).Item(0).ToString()
' Me.dg_ques.Rows(c).Cells(1).Text = rst3.Rows(c).Item(1).ToString

' p = rst3.Columns.Count
' q = 2
' While q < p - 1
' Me.dg_ques.Rows(c).Cells(q).Text = rst3.Rows(c).Item(q).ToString
' q = q + 1
' End While
' c = c + 1
'End While
con.Close()
here the format of the gridview is like this

qid qtxt op1 select op2 select op3 select op4 select
this is the format of my gridview
willington.d at 25-Feb-13 5:21am
   
I think you are in wrong way...
supriya931 at 25-Feb-13 5:22am
   
no you cant understand me
i want each option value will be display one checkbox in the gird view in each row
supriya931 at 25-Feb-13 5:26am
   
you will send me your gmail id i'll send you a file
willington.d at 25-Feb-13 5:26am
   
I understood. I said that, the way u write program is work. I'll make it work... give me some time :-)
willington.d at 25-Feb-13 7:30am
   
Hi... Could u plz send me the structure of two tables (ques_mst, option_mst ) to my ID..
so that i can debug ur program....
willington.d at 26-Feb-13 1:32am
   
you have only four options or more then four options?
supriya931 at 26-Feb-13 1:33am
   
i solved it but now the problem is how to select only one value from four of these in single row of the grid view and how to store it into dataset.
willington.d at 26-Feb-13 1:57am
   
great... u can use radiobutton instead of checkbox... it will allow u to select only one
supriya931 at 26-Feb-13 2:04am
   
no it will not work like that in gridview
willington.d at 26-Feb-13 3:16am
   
Hi Supriya

refer to the below link, u will get a answer:

http://www.aspforums.net/Threads/567701/Allow-to-user-to-check-only-one-checkbox-in-ASPNet-GridView-Row/

Regrads
Willington

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 476
1 Maciej Los 276
2 Sascha Lefèvre 275
3 Andy Lanng 263
4 OriginalGriff 215
0 Sergey Alexandrovich Kryukov 9,829
1 OriginalGriff 8,530
2 Sascha Lefèvre 3,579
3 Maciej Los 3,332
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100