How to use combobox in devexpress aspxgridview - asp.net

I am displaying my data in a devexpress gridview. One of the column is state value. When the grid is edited, I have to show the state in a combobox, so that the user could change the state by choosing a different state. Currently it is displayed in a textbox, since it is the default. Essentially when the user clicks the edit button, a combobox should be displayed as part of the edit controls, and the combobox should be populated with all possible states in the codebehind and the selected value should be the initial value on the grid. It is very easy do it in MS gridview. But I couldn't see any sample code for how to do it in the devexpress gridview.
Thanks

Use GridViewDataComboBoxColumn. Declare datasource and attach it to combo box column or populate it in code behind. This example contains both variants.
You can also take a look at DevExpress grid editing demos.

<dx:GridViewDataTextColumn FieldName="FieldName" VisibleIndex="4">
<EditItemTemplate>
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DataSourceID="newDataSource" >
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
You'll need to set the datasource so you'll get the list of values

If you don't have a data source and want to include the combo box items in your code, here's another way to create the column:
<dx:GridViewDataComboBoxColumn FieldName="QAAproval" VisibleIndex="11" Width="30px">
<PropertiesComboBox>`enter code here`
<Items>
<dx:ListEditItem Text="GENERIC" Value="GENERIC" />
<dx:ListEditItem Text="FAIR" Value="FAIR" />
<dx:ListEditItem Text="VSE" Value="VSE" />
<dx:ListEditItem Text="ECAV" Value="ECAV" />
<dx:ListEditItem Text="FMMDS" Value="FMMDS" />
<dx:ListEditItem Text="CLEAR" Value="CLEAR" />
</Items>
</PropertiesComboBox>
<CellStyle Font-Size="XX-Small">
</CellStyle>
</dx:GridViewDataComboBoxColumn>

Edit the GridView Template, and in the EditTemplate of the field, add the dropdownbox. It might come to look like this
<dx:GridViewDataTextColumn Caption="Field Name"
FieldName="FieldName" VisibleIndex="3">
<EditItemTemplate>
<cc1:DropDownList ID="DropDownList1" runat="server">
</cc1:DropDownList>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
So when you edit that row, it will show the DDL

I have used the following code to have combo box in the aspxgridview.
I hope this example helps :
<dx:GridViewDataComboBoxColumn FieldName="DatabaseFieldName" Settings-FilterMode="DisplayText"
Width="3%" VisibleIndex="3" Visible="True" Caption="Priority" Settings-AutoFilterCondition="Contains"
HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center" CellStyle-HorizontalAlign="Center"
CellStyle-VerticalAlign="Top">
<PropertiesComboBox ValueType="System.String" DataSourceID="objDataSourceID"
Width="200px" Height="25px" TextField="TextFieldName" ValueField="ValueFieldName"
IncrementalFilteringMode="StartsWith">
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>

Related

PNM Sequence & asp.net: GridTemplateColumn should be mandatory

I use PNM Sequence. And I need to make one grid column as the mandatory field.
I know how to make it with any separate control. E.g. I can type:
<sq8:GridBoundColumn DataField="txtField" HeaderText="txtField"
SortExpression="txtField" UniqueName="txtField" FilterControlAltText="">
<ColumnValidationSettings>
<RequiredFieldValidator ForeColor=""></RequiredFieldValidator>
</ColumnValidationSettings>
</sq8:GridBoundColumn>
And I can use this Validator for the TextBox:
<sq8:Label runat="server" Text="Field:" ID="Label1" Width="100%"></sq8:Label>
<nobr>
<sq8:TextBox runat="server" ID="txtField" Width="100%"></sq8:TextBox>
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtField"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
</nobr>
<sq:BindableControl runat="server" TargetControlID="txtField"
DataField="txtField"></sq:BindableControl>
And it works. User can't send the form because he gets an error - the field is empty.
But I need to do the same with grid.
When I open "Edit columns" in Grid Wizard I can't see any property as "mandatory" or something like this.
And the code with RequiredFieldValidator doesn't work with a grid column. If I try to use it:
<Columns>
<sq8:GridBoundColumn DataField="txtFieldGrid" HeaderText="txtFieldGrid"
SortExpression="txtFieldGrid" UniqueName="txtFieldGrid"
FilterControlAltText="">
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtFieldGrid"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
<sq:BindableControl runat="server" TargetControlID="txtFieldGrid"
DataField="txtFieldGrid"></sq:BindableControl>
</sq8:GridBoundColumn>
</Columns>
In this case, I have an error:
Is there some method for grid column validation? Or it's impossible with a grid?
Maybe I can use some javascript?

how to get assign values to check box in grid view

i want to know status is active or not in check box. how to bind check box with database data. while editing im lossing previous data. data is not showing in check box
code behind updating event
cmd.Parameters.AddWithValue("#LeadsAccess", ChkLeads.Checked);
<asp:TemplateField HeaderText="Leads">
<ItemTemplate>
<asp:CheckBox ID="ChkLeads" runat="server" />
</ItemTemplate>
</asp:TemplateField>
db column- [Leads]
You can try like this
<asp:CheckBox ID="ChkLeads" runat="server"
Checked='<%#bool.Parse(Eval("columnName").ToString())%>' />
try this..
you can bind the db value like this..
<asp:CheckBox ID="ChkLeads" runat="server" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("LeadsAccess"))%>'/>

How to find control nested into ASPXGridview on button click

I have a text box and button inside of my ASPXGridview, DetailRow view. So when it's expanded I need to type text and click button and on server side to get value from the textbox.
Here is my code. The problem is somewhere on postback I'm loosing text value. So text box is empty
protected void Button1_Click(object sender, EventArgs e)
{
ASPxGridView grid = EmailGridView;
for (int i = 0; i < grid.VisibleRowCount; i++)
{
var txtDescription = (ASPxMemo)grid.FindDetailRowTemplateControl(i, "txtDescription");
if(txtDescription != null)
{
var text = txtDescription.Text;
}
}
}
<dxwgv:ASPxGridView ID="EmailGridView" KeyFieldName="ThreadId" runat="server" AutoGenerateColumns="False"
OnHtmlRowCreated="EmailGridView_HtmlRowCreated" SettingsDetail-AllowOnlyOneMasterRowExpanded="true"
SettingsBehavior-ConfirmDelete="true" OnHtmlRowPrepared="EmailGridView_HtmlRowPrepared"
OnRowDeleted="EmailGridView_RowDeleted">
<SettingsBehavior ConfirmDelete="True" />
<Columns>
<dxwgv:GridViewDataTextColumn Caption="ID" FieldName="Id" VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewCommandColumn VisibleIndex="5" Caption=" ">
<DeleteButton Visible="True">
</DeleteButton>
</dxwgv:GridViewCommandColumn>
</Columns>
<SettingsDetail ShowDetailRow="true" />
<SettingsBehavior ConfirmDelete="True" />
<SettingsDetail AllowOnlyOneMasterRowExpanded="True" ShowDetailRow="True" />
<Templates>
<DetailRow>
<dxwgv:ASPxGridView ID="EmailSubGridView" Width="750px" OnBeforePerformDataSelect="EmailSubGridView_BeforePerformDataSelect"
runat="server" AutoGenerateColumns="False"
onhtmlrowcreated="EmailSubGridView_HtmlRowCreated">
<Columns>
<dxwgv:GridViewDataTextColumn Caption="MessFrom" FieldName="MessFrom">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="Message" FieldName="Message">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="SendDtm" FieldName="SendDtm">
</dxwgv:GridViewDataTextColumn>
</Columns>
<StylesEditors>
<ProgressBar Height="25px">
</ProgressBar>
</StylesEditors>
</dxwgv:ASPxGridView>
<dx:ASPxMemo ID="txtDescription" runat="server" Width="170px" Height="71px"></dx:ASPxMemo>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Reply" />
</DetailRow>
</Templates>
Conny,
The ASPxGridView has callbacks built into it so I recommend using that approach as it's easier and it gives you a better user experience. The find method you're using requires the visiblerowindex.
Take a look a this code central example to see how to get data from the server:
How to bind the detail GridView to data based on the end-user input
If you want these process when you are expanding your ASPxGridview, you should use DetailRowExpandedChanged event.
This event works when you expand or collapse your ASPxGridview.
Mehul's example is very good actually for this situation. After that, if you still have a problem, you can ask your question in Devexpress Support. They are really helpful and quick.

How to enable/disable the row double click editing property of radgrid based on condition

I have the following code
Javascript
function RowDblClick(sender, eventArgs)
{
window.radopen("SourceFileAdmin.aspx?SourceSystemFileId=" + eventArgs.getDataKeyValue("SourceSystemFileId"), "UserListDialog");
}
Aspx Grid Code
<telerik:RadGrid ID="RadGrid" runat="server" AllowPaging="True" AllowSorting="True"
GridLines="None" ShowGroupPanel="True" Skin="Black" AutoGenerateColumns="False"
Width="100%" AllowAutomaticInserts="false" AllowAutomaticUpdates="false" PagerStyle-Mode="NumericPages"
OnItemInserted="GridItemInserted"
onitemdatabound="GridItemDataBound">
.............................................
........................................
..................................................
<ClientSettings>
<Selecting AllowRowSelect="true" />
<ClientEvents OnRowDblClick="RowDblClick" />
</ClientSettings>
</telerik:RadGrid>
<asp:Button ID="AddSrcButton" runat="server" Text="Add New" OnClientClick="return ShowInsertForm();" />
My actual question is to disable or enable RowDblClick property based on the user permission
This is my sample code in the aspx.cs file where i want to set the edit or view permission for radgrid
this.AddSrcButton.Enabled = this.Privilige.CanModify;//the value will be true or false
this.RadGrid.Enabled=this.Privilige.CanModify;
//the value will be true or false ,based on this value i want to enable/disable my rowdblclick,can any one help out to figure out where i am doinfg wrong
You can set the row double click on the server side programatically
if(!this.Privilige.CanModify)
RadGrid.ClientSettings.ClientEvents.OnRowDblClick = "";
Beware though to renable the page you will probably need to refresh it all to get the clientside javascript to be loaded again, unless you move that into the .cs file

How do I data bind a drop down list in a gridview from a database table using VB?

So in this gridview, there is a column for status and I want to have a drop down list with Pass, Pending, Fail appear when the edit button is clicked. These values are already in a table, so I need to somehow bind from this table to each ddl for every row.
Here is the column from the gridview. As you can see, I would like to just have a label showing when not in edit mode, and a ddl when the edit button is pressed
<asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="Name"
datatextfield="Name" DataSource="*What goes here?*"> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text='I don't understand how to get this from the ddl' />
</ItemTemplate>
</asp:TemplateField>
For clarity's sake, my table is named Status, and the database is named DirectImport
There's a few steps to go through here - none of them are particularly difficult, but they can be a bit fiddly (IMHO). The good news is once you've got this working once, it gets easier to do it again!
I'm assuming you've got a <asp:*DataSource> control on your page - my preference is for an ObjectDataSource but I don't think it matters, I think a SqlDataSource works equally well. I've never tried doing this with GridView.DataSource = MyDataSet in code-behind, so I don't know whether that would work or not, but my assumption is that it wouldn't as you wouldn't get the proper two-way binding that you want. This data source feeds your grid with your main data. The key point here is that your database query must return both the Status text field and the Status id.
So your gridview will now look something like:
<asp:objectdatasource runat="server" id="MainDataSource" ... />
<asp:gridview runat="server" id="MyGridView" DataSourceID="MainDataSource">
<Columns>
<asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text="<%# Bind('Status') %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
The Text="<%# Bind('Status') %>" is the bit you're missing to get the status text into the grid.
Now add a second data source into your markup that reads in the set of values from the Status table.
<asp:objectdatasource runat="server" id="StatusObjectDataSource" ... />
And add the EditItemTemplate into the GridView, which is bound to the Status DataSource.
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="StatusID"
datatextfield="Status" DataSourceID="StatusObjectDataSource"
SelectedValue="<%# Bind('StatusId') %>" />
</EditItemTemplate>
The SelectedValue="<%# Bind('StatusId') %>" is what connects up the two datasets so that when you flip a row into Edit mode, the dropdownlist has the correct item already selected, and when you then save it you've got the Status ID to put into your database.
And you're done.
I have used the RowDataBound event. Here is a small code snippet. HTH
you would have an ItemTemplate in your aspx/ascx
<asp:TemplateField HeaderText="Column Headings">
<ItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" Width="150"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
and in your code behind, you will have
protected void grdDataMap_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlName");
ddl.DataSource = someList;//the source of your dropdown
ddl.DataBind();
}
}
so when you bind your grid with grdDataMap.Databind (assuming your grid id is grdDataMap), row databound event will be called for each row (including header/footer, and thats the reason you check RowType)
so you can probably decide what controls/columns to hide/show/bind inside this row databound event
In the winforms world I pull my objects from the DB into a List(Of Whatever) and use the list as the datasource.
This also lets me add extra "convenience" fields in the object so that I can populate it with stuff from other tables.
I don't know asp.net at all so if you can do something similar, it might help.
A really quick solution is to create a custom web control for the status dropdown. The control will always contain the same data. When it renders you populate the datasource. When it gets added to the gridview, the data will be in the drop down. Hope that helps!

Resources