|
hallo friends,
i wnt one column in my datagridview as a combobox & wnt to fill combobox by fetching values frm the database.
my question is how to add comboBox as a column in DatagridView through coding & how to fill it by fetching values frm database?
it is urgent please help me.
priya
|
|
|
|
|
First you have to fill the dataset as done in the below getdataset(). Then in the rowdatabound event of the Gridview bind Dropdownlist with the dataset. In the page_load event you only need to bind the GridView.
'Function to fill the dataset
Private Function getdataset() As DataSet
Dim connectionstring As String = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password="
Dim query As String = "select p.categoryid,p.productid, p.productname,c.categoryid,c.categoryname from products p,categories c where p.categoryid=c.categoryid and c.categoryid<3"
Dim myconnection As New SqlConnection(connectionstring)
Dim ad As New SqlDataAdapter(query, myconnection)
Dim ds As New DataSet()
ad.Fill(ds)
Return ds
End Function 'getdataset
******************************************************************************
'Code in rowdatabound event to bind DropDownList to GridView
Protected Sub gridview1_rowdatabound(sender As Object, e As GridViewRowEventArgs)
Dim mytable As New DataTable()
Dim productidcolumn As New DataColumn("productid")
Dim productnamecolumn As New DataColumn("productname")
mytable.Columns.Add(productidcolumn)
mytable.Columns.Add(productnamecolumn)
Dim ds As New DataSet()
ds = getdataset()
Dim categoryid As Integer = 0
Dim expression As String = String.Empty
If e.Row.RowType = DataControlRowType.DataRow Then
categoryid = Int32.Parse(e.Row.Cells(0).Text)
expression = "categoryid = " + categoryid
Dim ddl As DropDownList = CType(e.Row.FindControl("dropdownlist1"), DropDownList)
Dim rows As DataRow() = ds.Tables(0).Select(expression)
Dim row As DataRow
For Each row In rows
Dim newrow As DataRow = mytable.NewRow()
newrow("productid") = row("productid")
newrow("productname") = row("productname")
mytable.Rows.Add(newrow)
Next row
ddl.DataSource = mytable
ddl.DataTextField = "productname"
ddl.DataValueField = "productid"
ddl.DataBind()
End If
End Sub 'gridview1_rowdatabound
******************************************************************************
'Page Load event
Protected Sub Page_Load(sender As Object, e As EventArgs)
gridview1.DataSource = getdataset().Tables(0)
gridview1.DataBind()
End Sub 'Page_Load
******************************************************************************
{Souce}
GridviewNestingDropdowlist
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="False" onrowdatabound="gridview1_rowdatabound">
<columns><asp:boundfield datafield="categoryid" headertext="categoryid">
<asp:boundfield datafield="categoryname" headertext="category name">
<asp:boundfield datafield="productid" headertext="productid">
<asp:templatefield headertext="products">
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server">
|
|
|
|
|
Good Morning,
Thnank you so much for your help. but i didnt see page load event in my form....i think this coding is for asp.net ....& i m developing application in vb.net(Winforms)!
So please if you have such coding in vb.net application Please help me.
priya
|
|
|
|
|
hi....
u can do this it worked for me
call this creategrid func from formload()
Public Sub creategrid()
'Declare and initialize local variables used
Dim dtCol As DataColumn = Nothing 'Data Column variable
'Create the String array object, initialize the array with the column names to be displayed
'arrstr = New String(9) {"Start Date", "End Date", "Start Time", "End Time", "Price", "Least Count", "Valid Days", "Remarks", "Booked slots", "Delete"}
arrstr = New String(8) {"Start Date", "End Date", "Start Time", "End Time", "Price", "Least Count", "Valid Days", "Remarks", "Booked slots"}
'Create the Data Table object which will then be used to hold columns and rows
dataTable = New DataTable("Controls")
'Add the string array of columns to the DataColumn object
Dim icnt As Integer
Try
For icnt = 0 To arrstr.Length - 1
Dim str As String = arrstr(icnt)
dtCol = New DataColumn(str)
dtCol.DataType = System.Type.GetType("System.String")
dtCol.DefaultValue = ""
dataTable.Columns.Add(dtCol)
Next icnt
If sAction = "Add" Then
Dim blrow As DataRow
blrow = dataTable.NewRow
dataTable.Rows.Add(blrow)
End If
'Set the Data Grid Source as the Data Table created above
dgAddInv.DataSource = dataTable
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(dgAddInv.DataSource, dgAddInv.DataMember), CurrencyManager)
CType(cm.List, DataView).AllowNew = False
'set style property when first time the grid loads, next time onwards it will maintain its property
If Not dgAddInv.TableStyles.Contains("Controls") Then
'Create a DataGridTableStyle object
'Set its properties
dgdtblStyle.MappingName = dataTable.TableName
dgdtblStyle.RowHeadersVisible = True
dgAddInv.TableStyles.Add(dgdtblStyle)
Dim colStyle As GridColumnStylesCollection
colStyle = dgAddInv.TableStyles(0).GridColumnStyles
colStyle(8).ReadOnly = True
colStyle(8).NullText = "--New--"
'colStyle(1).Width = 220
'dgdtblStyle.RowHeadersVisible = False
dgdtblStyle.PreferredRowHeight = 22
dgdtblStyle.PreferredColumnWidth = dgdtblStyle.PreferredColumnWidth + 10
dgAddInv.BackgroundColor = Color.White
End If
'Take the text box from the second column of the grid where u will be adding the controls of your choice
datagridtextBox = CType(dgAddInv.TableStyles(0).GridColumnStyles(0), DataGridTextBoxColumn)
datagridtextBox1 = CType(dgAddInv.TableStyles(0).GridColumnStyles(1), DataGridTextBoxColumn)
datagridtextBox2 = CType(dgAddInv.TableStyles(0).GridColumnStyles(2), DataGridTextBoxColumn)
datagridtextBox3 = CType(dgAddInv.TableStyles(0).GridColumnStyles(3), DataGridTextBoxColumn)
datagridtextBox4 = CType(dgAddInv.TableStyles(0).GridColumnStyles(6), DataGridTextBoxColumn)
'for least count and price
datagridtextBox5 = CType(dgAddInv.TableStyles(0).GridColumnStyles(4), DataGridTextBoxColumn)
datagridtextBox6 = CType(dgAddInv.TableStyles(0).GridColumnStyles(5), DataGridTextBoxColumn)
Catch oexp As Exception
ShowMsgBoxErrorOk(oexp)
End Try
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''and then addd ur controls in mouse down event as below
Private Sub DGAddInv_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgAddInv.MouseDown
hitTestGrid = dgAddInv.HitTest(e.X, e.Y)
If Not (hitTestGrid Is Nothing) Then
If hitTestGrid.Type = DataGrid.HitTestType.Cell Then
dtp = New DateTimePicker
dtp.Dock = DockStyle.Fill
dtp.Cursor = Cursors.Arrow
dtp.Format = DateTimePickerFormat.Custom
dtp.CustomFormat = "MM/dd/yyyy"
dtp.ShowUpDown = False
For i = 0 To dataTable.Columns.Count - 1
sType = dgAddInv(0, i).ToString()
If hitTestGrid.Column = i Then
Select Case hitTestGrid.Column
'Add and show the control of ur choice
Case 0
datagridtextBox.TextBox.Controls.Add(dtp)
If dgAddInv.Item(hitTestGrid.Row, 0) <> "" Then
dtp.Value = CType(dgAddInv.Item(hitTestGrid.Row, 0), Date).ToShortDateString
End If
dtp.BringToFront()
End Select
End If
datagridtextBox.TextBox.BackColor = Color.White
Next i
end if
end if
end sub
see if that works for u ...u will have to change a bit as per your control as i am adding a datetime picker u can add a combo instead....
-- modified at 2:33 Thursday 25th January, 2007
" There are 10 types of people one who understands binary and one who don't................."
|
|
|
|
|
I am sorry about posting ASP code.
You can not set a datatable or dataview as the datasource for a combobox inside a Grid. In the following code I have given an ArrayList as the datasource for it. You can easily store a column into an ArrayList that is also done here. Please note that you have to set the AutoGenerateColumns property of the Grid to 'false' and add col's manually. I hope you know how to do that. The columns from the table 'product' which is in the database are: 'pname' and 'price'. And the manually added colmn names of the Grid 'Dg_Test' are: 'pname', 'Price' and 'type'. The third column in Dg_Test is the one you want that is of type DataGridViewComboBoxColumn. Other two pname and Price are DataGridViewTextBoxColumn. The value for third col'n is taken from a different table 'producttype' with field 'type'. I have not added any code regarding database connectivity for OleDbConnection.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Fill_Grid()
End Sub
Private Sub Fill_Grid()
Dim stt As String = "Select * from product"
Dim Cmd As OleDbCommand
Dim Adaptor As OleDbDataAdapter
Dim Ds As New DataSet
Dim dt As DataTable
Dim arList As New ArrayList
'Add columns manually using smart tag of the GridView column names are pname,Price,type
Dg_Test.AutoGenerateColumns = False
dbObj.OpenConnection()
Cmd = dbObj.ReturnOledbCommand(stt)
Adaptor = New OleDbDataAdapter(Cmd)
Adaptor.Fill(Ds, "product")
dt = Ds.Tables("product")
Me.Dg_Test.DataSource = dt
'Type cast the column pname and Price to DataGridViewTextBoxColumn and set the DataPropertyName to the
'field names in the database
Dim Name As DataGridViewTextBoxColumn = CType(Dg_Test.Columns("pname"), DataGridViewTextBoxColumn)
Name.DataPropertyName = "pname" 'pname is the field from the table in database
Dim Price As DataGridViewTextBoxColumn = CType(Dg_Test.Columns("Price"), DataGridViewTextBoxColumn)
Price.DataPropertyName = "price" 'price is the field from the table in database
'Datasource for DataGridViewComboBoxColumn
stt = "Select type from producttype"
Cmd = dbObj.ReturnOledbCommand(stt)
Adaptor = New OleDbDataAdapter(Cmd)
Adaptor.Fill(Ds, "producttype")
dt = Ds.Tables("producttype")
'Insert elemants into array list from dt
For Each dr As DataRow In dt.Rows
arList.Add(dr(0)) 'dr contains only one column
Next
Dim Type As DataGridViewComboBoxColumn = CType(Dg_Test.Columns("type"), DataGridViewComboBoxColumn)
Type.DataSource = arList 'Datasource is set to an arraylist
dbObj.CloseConnection()
End Sub
|
|
|
|
|
I am inserting values in a datagridview bounded to a dataset. It working normally. If i click on an empty row on the datagriview and did not enter a value, when i try to insert in the database, it is inserting a null value in the database although it is not present in the dataset.
Database error
|
|
|
|
|
hey u can use the currency manager ..it keeps the grid enabled but stops adding new rows in the grid...probably the best option for u ....here is the code...to do that...
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(dgAddInv.DataSource, dgAddInv.DataMember), CurrencyManager)
CType(cm.List, DataView).AllowNew = False
it worked for me .........
" There are 10 types of people one who understands binary and one who don't................."
|
|
|
|
|
Thx for ur help. I tried ur code, it did not work with me. It forbids me to add a now row in the datagridview at runtime. I need it to allow me add rows at runtime but if i clicked on a new row and did not enter values in it, i want it not to insert it in the database. Help!
It did not work.
|
|
|
|
|
hey well then we have a work around that u can try......once user adds a blank row and clicks on save with nulls in that row ....delete that row from datatable as follows...
lets say actual datatable name is dataTable
and the temp one we are creating for manipulating is temptable
temptable = dataTable
For dtblnkrowcnt = 0 To dataTable.Rows.Count - 1
For colcount = 0 To dataTable.Columns.Count - 2
'u can check for blank or system.dbnull.value as suitable.....
If dataTable.Rows(dtblnkrowcnt).Item(colcount) = "" Then
blankcount = blankcount + 1
End If
Next
If blankcount = dataTable.Columns.Count - 1 Then
temptable.Rows(dtblnkrowcnt).Delete()
End If
blankcount = 0
Next
temptable.AcceptChanges()
dataTable = temptable
i know its just a workaround but it should fix up the issue ....best of luck dude.....
" There are 10 types of people one who understands binary and one who don't................."
|
|
|
|
|
I am inserting values in a datagridview bounded to a dataset. It working normally. If i click on an empty row on the datagriview and did not enter a value, when i try to insert in the database, it is inserting a null value in the database although it is not present in the dataset.
|
|
|
|
|
DataGrid1.Enabled = False
that is worked
Ahmed El-Badry
|
|
|
|
|
Thx for ur help . But i tried it before and it did not work. I need to keep the datagridview enabled to add rows at run time but i do not want it to add null rows.
Thx but it did not work.
|
|
|
|
|
i have a form and i set transparency key on orange color, and my form background is orange same color and i placed png image that have shadows and other but it doesnd transparent it fully becose of shadows... is it possible to set it fully transparent? or i need to remove shadows?
thanks
sorry on bad english
|
|
|
|
|
If you want to transparent an image with transparencykey, dont set the form backcolor to same as the transparencykey, because only the form will be transparent. And also check whether the image color you want to transparent same as the transparencykey.
Sorry on bad english too.
Experience is the best teacher
|
|
|
|
|
|
i want to create a batch file that compiles the project and generates the dll for the same (vb and vb.net projects)....scratchin my head since morning ...need help ...
there are 10 types of people one who understands binary and one who don't
|
|
|
|
|
if you want run some command type thes code
Shell(your command)
Shell("Ping 10.0.0.1")
if you want to hid action type
Shell("ping 10.0.0.1", AppWinStyle.Hide)
Ahmed El-Badry
|
|
|
|
|
okies ...well yes that is if i want to run my commands directly from the code...
rght ...well i was waiting ...for the reply i figured out myself what i was looking for ..
we can create a batch file ...with foll code
cd C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE
cls
devenv /build Debug "C:\Documents and Settings\atul.sharma\My Documents\Visual Studio Projects\YMSEntity\ymsentity.sln"
devenv /build Debug "C:\Documents and Settings\atul.sharma\My Documents\Visual Studio Projects\ClassLibrary2\classlibrary2.sln"
This way i can build the dlls of multiple projects in a single go .....thanx buddy for ur time and thought...
" There are 10 types of people one who understands binary and one who don't................."
|
|
|
|
|
I did the file watcher that catches deletes,renames,new files but:
Is it possible that you watch if someone moves files or even if i place a "D:" disk to watch than if someone ower network access that drive on my computer that it pop up message?
Please if you can help me on any way please help me...
|
|
|
|
|
Hello,
I am Using VS.net 2005. in this i was create custom textbox control for when textbox got fosus then textbox's font will be bold and green colour. that is working.
but now problem is this. i am unable to input data into textbox from database. because it is not working as a textbox. and not get textbox property as we want to set password char.
Please help me.
may be control is not created right method.
if anybody know to create coustom textbox for when textbox got fosus then textbox's font will be bold and green colour. plese tell me.
Thank You.
Vikash Yadav
|
|
|
|
|
How have you done this ? Have you derived a class from the TextBox ? If so, it IS a textbox. If not, why not ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Thank You For Help.
But i want to tell you i am fresher. i dont't know that how i can create derived class from the textbox. can u help me. Pleaze send code if you have to create custon control textbox.in that i can change font size and colour.
Thanks & Regards
Form :-
Vikash Yadav
|
|
|
|
|
Vikash Yadav wrote: i dont't know that how i can create derived class from the textbox.
OK, then stop writing GUI code and work through a book on the absolute basics.
Vikash Yadav wrote: Pleaze send code if you have to create custon control textbox.
I haven't. But, it's pretty simple. A lot simpler than what it sounds like you've done. Just create a class, make the TextBox the base class, and change the text color based on if you have the focus or not.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Tanks for a lot of help
Thanks & Regards
Form :-
Vikash Yadav
|
|
|
|
|
well,
in my winform i have some datagrids and one these must have no scrollbars, i've to remove the scrollbars that automatically apeears...
how can i do this????
here the code
Dim hhh As Integer
For hhh = 0 To DataGridX.Controls.Count - 1
If DataGridX.Controls(hhh).GetType Is GetType(VScrollBar) Then
DataGridX.Controls(hhh).Visible = False
DataGridX.Controls(hhh).Width = 0
End If
If DataGridX.Controls(hhh).GetType Is GetType(HScrollBar) Then
DataGridX.Controls(hhh).Visible = False
DataGridX.Controls(hhh).Width = 0
End If
Next
where is my error????
thenk's a lot
|
|
|
|