Multiple Column DropdownList for the ASP.NET DATAGRID

xiaoxiao2021-04-08  455

Image Showing When a Control IS Added to EditItemPlate.

Introduction

Based on my previous control "Multiple Column DropDownList for ASP.NET", I received many emails asking for the same control to be used in the DataGrid for web applications. Here we go .. This control can be used as the regular MS DropDownList in the DataGrid and also as a regular dropdownlist. It has all the properties, like DataTextField, DataValueField, DataSource, SelectedIndex etc. The download file contains the samples both in VB.NET and C #. in this sample, I have used the Northwind database of SQL Server.

Building the control

The Following Web Server Controls Were Used to Build this Control:

TextBox Label Panel

Adding The Control to DataGrid

.

.. Drop a DataGrid onto your webform Right click the DataGrid and select Property Builder and select Columns A bound column may or may not be required, I have shown samples of both Add two bound columns, set the header and the text field:. SupplierID and CompanyName. Add one Template Column and set the header text to Products. Uncheck the checkbox "Create columns automatically at run time". Click OK. now right click the DataGrid and select Edit Template, now either you could add this in ItemTemplate or EditItemTemplate Set The Properties of The Control, Like Cssclass, GridrowColor, DataTextField etc. Close The Template Box.

Points to Note:

The DataValueField property is readonly, which means you can not set the value, so make sure that in your query, the row value field is always the first column, which will not be displayed in the dropdown. The DataTextField returns an integer value, so while setting this property, type the column position from your "SELECT" statement, which needs to get displayed in the textbox. (For eg, to display the second column "LastName" in the textbox, set DataValueField = 2.) If your DataGrid is inside the "Table / Div / Span", then make sure the "Position" attribute in the "Style" property is set to absolute, and if not used in any of these tags, then set the DataGrid's Style property.Make sure that the Horizontalalign and Vertyln Properties of The ItemStyle of Your DataGrid IS SET AS SHOWN, ELSE The Control May Not Be posationed Properly Inside The DataGrid. EG:

See the code below. E.G., To Populate The Dropdown with firstname and lastname:

Select Employeei, Firstname, Lastname

From Employees

Now if added in the edititemtemplate (BASEDOMN MAY OR MY NOT BE Required), in My Sample I buy Two s:

Font-size = "8pt" headerstyle-forecolor = "tan"

AutogenerateColumns = "false" Headerstyle-BackColor = "Maroon"

Headerstyle-font-bold = "true" font-name = "verdana" showfooter = "false"

Bordercolor = "Tan" DatakeyField = "SupplierID"

OnupdateCommand = "mydatagrid_update" oncancelcommand = "mydatagrid_cancel" OneDitCommand = "MyDataGrid_edit" cellspacing = "0" cellpadding = "3"

Width = "800" style = "Position: absolute; top: 0px; left: 0px">

Updatetext = "update" canceltext = "ca Zan" edittext = "edit">

Readonly = "True" Headertext = "Supplier ID">

Readonly = "true" Headertext = "Company Name">

<% # Databinder.eval (container.dataitem, "productname")%>

Width = "200"

SELECTEDINDEX = '<% # databinder.eval (container.dataitem, "productid")%>'

CssClass = "cStyle" runat = "server"

DataSource = "<% # populateddlist ()%>"

DataTextField = "2">

If you want to show the existing value the "SELECTEDEX". "SELECTEDEX". "SELECTEDEX"

PROPERTIES

DataTextField - The field to be shown in the Ccontrol DataValueField -. Read only property (value field to identify the row - default is first column {0}) DataSource -. DataSet to populate the dropdown DDTextboxReadonly -. If false, can type your own text GridRowColor -. OnMouseMove changes the row color GridTextColor -. OnMouseMove changes the text color ListBoxHeight -. Sets the height of the dropdown ReturnsValue -. to get the text, set it to false to get the value, set it to true, while. Updating. SELECTEDINDEX - IF SET, Shows The EXISTING VALUE IN THE DROPDOWN.

Populating the datagrid

Private Void Page_Load (Object Sender, System.EventArgs E)

{

// put user code to initialize the page

IF (! ispostback)

{

BinddataGrid ();

}

}

protected void binddatagrid ()

{

String Sqlstr = "Select S.comPanyName, S.supplierId,"

"P.ProductName, P.ProductID"

"From Suppliers S Inner Join Products P"

"on s.supplierid = p.supplierid";

SQLDA = New SqldataAdapter (Sqlstr, SqlCon);

DS = new dataset ();

SQLDA.FILL (DS, "Prod");

JskdataGrid.dataSource = ds.tables ["prod"];

JSKDataGrid.DATABIND ();

}

Populating the dropdownlist

Public Dataset PopulateDList () {

String Sqlstring = "SELECT ProductID, ProductName,"

"CategoryName As Name, Unitprice As Price"

"From Products P Inner Join Categories C"

"on p.categoryid = c.categoryid";

SqlDataAdapter ad = new sqldataadapter (sqlstring, sqlcon);

DataSet DS = New Dataset ();

Ad.fill (DS, "categories");

Return DS;

}

Codes to Edit / Update / Cancel

Protected Void JskdataGrid_EDIt (Object Sender, DataGridCommandeventArgs E)

{

JskdataGrid.edititeMindex = E.Item.itemindex;

BinddataGrid ();

}

Protected Void JskdataGrid_Cancel (Object Sender, DataGridCommandeventArgs E)

{

JSKDataGrid.editItemIndex = -1;

BinddataGrid ();

}

Protected Void JSKDataGrid_Update (Object Sender, DataGridCommandeventArgs E)

{

Try

{

String itemvalue;

String itemtext;

// TO GET the DATAVALUEFIELD of the DROPDOWN

(DDList.mcddlist) E.Item.FindControl ("McDDList1")). ReturnsValue = true;

ItemValue =

(DDList.mcddlist) E.Item.FindControl ("mcddlist1")). Text.toString ();

// to get the dattextfield of the dropdown

(DDList.mcddlist) E.Item.FindControl ("mcddlist1")). ReturnsValue = false;

itemtext =

(DDList.mcddlist) E.Item.FindControl ("mcddlist1")). Text.toString ();

// Write your Update Query

// update table set col1 = itemtext where col2 = itemvalue

// Execute the query

JSKDataGrid.editItemIndex = -1;

BinddataGrid ();

}

Catch (Exception EX)

{

Response.write (ex.Message);

}

Finally

{

/ * Close Your connection * /

}

}

Points of interest

The control is built mostly using JavaScript. There is a property "DDTextboxReadonly", this property is used to enable / disable the textbox, means either you could select the text from the list or you could type.Using the Intellisense

As in My Previous Article, I showed how to use the intelliSense. The zip file contacts the .xsd, copy it to the folowing location:

C: / Program Files / Microsoft Visual Studio .NET 2003 / Common7 / Packages / Schemas / XML

AND in the Tag of Your ASPX Page, Add The Following Code:

XMLns: jsk = "urn: http://schemas.ksjcontrols.com/dgdd/ASPNET">

History

Ver Version (Posted 12th May 2005) Ver. (1.1) - New Property Has Been Added, ListBoxHeight To Adjust The Height Of The Dropdown. Ver. (1.2) - Control Does Not Expand The DataGrid Rows.

转载请注明原文地址:https://www.9cbs.com/read-132806.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.046, SQL: 9