Asp.Net GridView adding Template Field Columns at Runtime - datagridviewcolumn

I am using GridView in ASP.NET with C#. I am adding Templete Field Columns to GridView on Runtime, Columns are added successfully with the required text in rows. But after filling GridView perform any button click event controls from rows removes, in rows i have Literal and Textbox controls in Templete Field as a ItemTempete.....
Any Help...

<asp:GridView runat="server" ID="gridView">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkTest"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in your rowdatabound event you can find it and do whatever you want
void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Entity entity = e.Row.DataItem as Entity;
LinkButton lnkTest = e.Row.FindControl("lnkTest") as LinkButton;
lnkTest.CommandArgument = entity.ID.ToString();
lnkTest.Text = entity.Name;
}
}

Related

How to avoid too many properties when binding Textbox details

I have a mvvm app with 22 textboxes.
In my "normal" wpf app I could make a list and then change settings to all the boxes likes this:
Brush MyBrush = Brushes.LightGray;
foreach (var a in AllBoxes)
{
a.Background = MyBrush;
a.IsReadOnly = true;
a.IsTabStop = false;
}
So in mvvm I need to make 3 bindings per textbox and create properties like:
private Brush _clr2;
public Brush Clr2
{
get { return _clr2; }
set
{
_clr2 = value;
NotifyOfPropertyChange(() => Clr2);
}
}
Is there an easier way to do this and not make 66 properties?
You create an UserControl for one TextBox with all bindings...with the ViewModel associated
<StackPanel>
<TextBox .... />
</StackPanel>
Then you use ObservableCollection of your ViewModel and Create a ParentView with ItemControl:
<ItemsControl ItemsSource="{Binding ListOfTextBox}">
And create an itemstemplate for the items in your ItemsControl
<DataTemplate>
<ContentControl cal:View.Model="{Binding}" />
</DataTemplate>

ComboBox in UI5 does not display ValueState

ComboBox is not showing state like Error, Warning with highlight around the borders. But it does change the state. For example, if it is error state, and if I try to enter new value in combobox, it will show that "invalid Entry" tip near the box. But the box borders are never highlighted in red. Below is the code:
XML.view
<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:l="sap.ui.layout">
<Dialog id......>
<ComboBox id="combo1" change="cChanged" items="{path: '/results'}">
<items>
<core:Item key="{ID}" text="{Name}"/>
</items>
</ComboBox>
</Dialog>
Controller.js
cChanged: function(oEvent) {
var newval = oEvent.getParameter("newValue");
var key = oEvent.getSource().getSelectedItem();
if (newval !== "" && key === null) {
sap.ui.getCore().byId("combo1").setValueState("Error");
oEvent.getSource().setValue("");
sap.m.MessageToast.show("Please select from existing IDs")
flag = false;
} else {
oEvent.getSource().setValueState('None');
}
You can also access combo1 control instance by using oEvent.getSource() event OR use byId() from the sap.ui.core.Fragment class and not sap.ui.getCore().byId()
Also, if you are writing a logic only to validate if what the user input in the combobox is a valid item, consider replacing your ComboBox by the sap.m.Select control.
Both ComboBox and Select has same look and feel, but Select does not allow a manual input. It can also have an empty option if you use the property forceSelection

Checkbox column in ejgrid syncfusion

I am Using SyncFusion ejGrid i.e. in my project.
I want to show Checkbox in first column to select/unselect multiple rows.
One checkbox in the column header to select/unselect all.
To add checkbox to Grid Column content we can use “templateId” property of columns and to add checkbox to Column header we can use “headerTemplateId” property. Then in checkbox click event we can select row by using “selectRows” method of Grid.
<script type="text/x-jsrender" id="check">
<input type="checkbox" class="rowCheckbox" id="chk" />
</script>
<script type="text/x-jsrender" id="head">
<input type="checkbox" id="headchk" />
</script>
$("#Grid").ejGrid({
...
columns: [
{ headerTemplateId: "#head", columnTemplate: true, templateId: "#check" },
...
});
$("#headchk").change(function () {
$("#Grid .rowCheckbox").on("change", checkChange);
gridObj = $("#Grid").data("ejGrid");
if ($("#headchk").is(':checked')) {// TO Select all rows in Grid Content
…
gridObj.selectRows(0, gridObj.model.pageSettings.pageSize);
}
else { // To remove selection for all rows
…
gridObj.cleanUpSelection();
}
});
function checkChange(e) {
…
//For MultiSelection using Checkbox
gridObj._multiSelectCtrlRequest = true;
}
I have created a sample based on your requirement and the same can be downloaded from below link.
Sample: http://www.syncfusion.com/downloads/support/directtrac/125963/grid898060682.zip

MVVM DataGrid copy information from selected cells

I’m using .Net 4.0 DataGrid with MVVM pattern. I need to enable user to select cells and copy information from selected cells to other DataGrid rows (either via keyboard shortcut or context-menu copy/paste). I tried to achieve this via SelectedItem or sending SelectedItem as CommandParameter but this functions only with the whole row and not with cells.
(DataGrid is bound to ObservableCollection that contains objects with float fields. These fields are then mapped to DataGrid cells)
So, is there any solution in WPF MVVM how to copy DataGrid cells from one row to another?
Thanks in advance
xaml:
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="9" AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Left"
Name="dataGrid" VerticalAlignment="Top" Width="{Binding ElementName=grid4,Path=Width}"
ScrollViewer.CanContentScroll="False" FrozenColumnCount="1" SelectionUnit="Cell" SelectionMode="Extended" CanUserSortColumns = "False"
CanUserReorderColumns="False" CanUserResizeRows="False" RowHeight="25" RowBackground="LightGray" AlternatingRowBackground="White"
ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible"
ItemsSource="{Binding Layers, Mode=TwoWay}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Selection, Mode=TwoWay}">
<DataGrid.InputBindings>
<KeyBinding Gesture="Shift" Command="{Binding ItemHandler}" CommandParameter="{Binding ElementName=dataGrid, Path=SelectedItems}"></KeyBinding>
</DataGrid.InputBindings>
ViewModel:
private float _selection = 0.0f;
public float Selection
{
get
{
return _selection;
}
set
{
if (_selection != value)
{
_selection = value;
NotifyPropertyChanged("Selection");
}
}
}
...
public DelegateCommand<IList> SelectionChangedCommand = new DelegateCommand<IList>(
items =>
{
if (items == null)
{
NumberOfItemsSelected = 0;
return;
}
NumberOfItemsSelected = items.Count;
});
public ICommand ItemHandler
{
get
{
return SelectionChangedCommand;
}
}
I think you might be after the SelectionUnit property. Setting this to CellOrRowHeader changes the selection method from full row to a single cell.
You do lose the nice "what row am I on?" highlighting but you are focusing on a single cell. (You could probably extend the datagrid to add your own highlight with the current row logic.)
<DataGrid AutoGenerateColumns="True" Grid.Column="1" Grid.Row="0"
HorizontalAlignment="Stretch" Name="dataGrid" VerticalAlignment="Stretch"
DataContext="{Binding}" ItemsSource="{Binding Path=MyDataTable}"
IsReadOnly="True" SelectionMode="Extended" SelectionUnit="CellOrRowHeader" />

AjaxControlToolkit CalendarExtender selected value is null

I try to get date from CalendarExtender in two ways: one with updatepanel and one without but it doesn't work. value of this two calendar extender are null.
It's weird because I can select date from this extenders, and text of textboxes are set to selected date.
How to fix it ?
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbPlannedStart"
Format="d">
</asp:CalendarExtender>
<asp:TextBox ID="tbPlannedStart" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="tbPlannedEnd"
Format="d">
</asp:CalendarExtender>
<asp:TextBox ID="tbPlannedEnd" runat="server"></asp:TextBox>
protected void btnAddProject_Click(object sender, EventArgs e)
{
var service = new Service1Client("WSHttpBinding_IService13");
var project = new MyProject();
project.PlannedEnd = CalendarExtender2.SelectedDate;
project.PlannedStart = CalendarExtender1.SelectedDate;
service.AddProject(project);
}
It solved my problem:
project.PlannedEnd = System.Convert.ToDateTime(tbPlannedEnd.Text);
project.PlannedStart = System.Convert.ToDateTime(tbPlannedStart.Text);