Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 24-Feb-13 17:36pm
v2
Comments
willington.d 25-Feb-13 4:18am    
Couldn't understand. Do u want to display checkbox if the field contains 1,2,3,4?
supriya931 25-Feb-13 4:33am    
no i want that checkbox will be display with the option value in the grid view
willington.d 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 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 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?

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