I am binding Checkbox field to database and want to show actual value like if in database it is 1 then checkbox should be ticked in gridview else unchecked. I tried but my code doesn't show avtual value in checkbox, always remain unchecked.
CODE:
<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" runat="server" AutoGenerateColumns="false" CssClass="mGrid" Width="450px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />
<%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
<asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" />
<asp:BoundField HeaderText="Message" DataField="Message" />
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" OnCheckedChanged="ckboxIsComplaint_CheckedChanged" AutoPostBack="true" runat="server" Value='<%# Eval("IsComplaint") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs
SELECT [ID]
,REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 106), ' ','/') as RecievingDate
,[ToMobileNo]
,[FromMobileNo]
,[Message],
[IsComplaint]
FROM [CmsSMSDb].[dbo].[ReceivedMessages]
where Convert(date,ReceivedDateTime)>= #DateFrom AND Convert(date,ReceivedDateTime)<= #DateTo AND IsComplaint=1
If you will get only true or false then you can use the following, by adding
Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' to the CheckBox.
<asp:CheckBox ID="ckboxIsComplaint" OnCheckedChanged="ckboxIsComplaint_CheckedChanged" AutoPostBack="true" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
If you are getting null value also, then you need to do the checking and unchecking from C# code in RowDataBound event.
Try this
<asp:CheckBoxField DataField="IsComplaint" HeaderText="IsComplaint" SortExpression="IsComplaint" />
instead of using
<asp:TemplateField HeaderText="IsComplaint"> ............. </asp:TemplateField>
Related
I have a problem in gridview, as per requirement i have set No of Records per page = 4 in gridview. I have to select Checkbox against every complaint but problem is then when i got to next pge in gridview and e.g fro 1 to 2 then when i come back to page 1 then it doesn't show TICK in check boxes . It doesn't remember my selection when i browse to and back to page.
<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="4" runat="server" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333" Width="550px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />
<%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
<asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" />
<asp:BoundField HeaderText="Message" DataField="Message" >
<ItemStyle Wrap="True" />
</asp:BoundField>
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
please check the above link.here your problem was clearly explained here.i think so it may be helpfull
As per comments...
If you do not update the underlying database by processing the OnCheckChanged event of the check box, then it will simply be reading the same data all the time.
From How to add event for Checkbox click in Asp.net Gridview Column, I have extracted the required information and tried to modify to fit your initial question.
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true/>
</ItemTemplate>
</asp:TemplateField>
add checkbox change event in aspx.cs page
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
<your data source>.Rows[row.DataItemIndex]["B"] = ((CheckBox)GridViewSmsComplaints.Rows[row.RowIndex].FindControl("ckboxIsComplaint")).Checked;
}
How can I fix the width for each column in GridView. My GridView keeps extend along with data in the cell. I need it to skip to new line when it reach the right side of the cell. This is my code in .aspx file:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="Emp_ID" DataSourceID="LinqDataSource1"
AllowPaging="True" AllowSorting="True" Width="900px" HorizontalAlign="Center" >
<Columns>
<asp:BoundField DataField="Emp_ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True" SortExpression="Emp_ID"/>
<asp:BoundField DataField="Emp_Username"
HeaderText="Username" SortExpression="Emp_Username" />
<asp:BoundField DataField="Emp_Password"
HeaderText="Password" SortExpression="Emp_Password" />
<asp:BoundField DataField="Emp_Name"
HeaderText="ชื่อพนักงาน" SortExpression="Emp_Name" />
<asp:BoundField DataField="Emp_Address"
HeaderText="ที่อยู่" SortExpression="Emp_Address" />
<asp:BoundField DataField="Emp_Tel"
HeaderText="เบอร์โทรศัพท์" SortExpression="Emp_Tel" />
<asp:TemplateField HeaderText="รูปพนักงาน" SortExpression="Emp_Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Emp_Picture") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image2" runat="server" Height="70px"
ImageUrl='<%# Eval("Emp_Picture", "{0}") %>' Width="50px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Status" HeaderText="สถานะ"
SortExpression="Status" />
<asp:HyperLinkField DataNavigateUrlFields="Emp_ID"
DataNavigateUrlFormatString="AdminUpdate.aspx?Emp_ID={0}" Text="Edit" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="ChkSelectAll" runat="server"
AutoPostBack="True" oncheckedchanged="ChkSelectAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="Chk" runat="server" AutoPostBack="True" oncheckedchanged="Chk_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's my design view:
As you can see, the first 4 columns has no problem with that until column 5. It keeps extend cause all GridView extend out of the page
Set itemstyle width of every column.For example.
<asp:BoundField DataField="Emp_Address"
HeaderText="ที่อยู่" SortExpression="Emp_Address">
<ItemStyle Width="200px" HorizontalAlign="Left" />
</asp:BoundField>
Wrapping is done based on words so that any single word is not broken in two lines.
So won't work for you if your data has long string without space.
The problem is that you can't insert spaces on your own because that may change the meaning of data.
While showing in grid we show only that long string which can be accommodated in column and then add "..." (only if string is more than what is showing) and then add a tooltip to show the full string.
That way the grid formatting is not compromised and if user wants he can hover his mouse and see the whole string.
hope this helps Example
Try setting the property RowStyle-Wrap of the GridView to True.
You can use ItemStyle-Width to fix the column width ,but in case you enter characters which can not be enclosed in given width (As seen in your image) ,column will expand as per the inputs so you can use RowStyle-Wrap property of gridview
<asp:GridView ID="grdVwtrial" runat="server" RowStyle-Wrap="true">
RowStyle-Wrap==true should work I guess. For more clarifications refer this:
http://forums.asp.net/t/1263769.aspx
I'm trying to add a delete column to my gridview, however when I press the delete button on a row, it calls the function I assigned to onRowDeleting infinitely or so it seems.
Here's my code:
<asp:GridView ID="GridView1" runat="server" CssClass="yep" AutoGenerateColumns="False" OnRowDeleting="dismissClick">
<Columns>
<asp:TemplateField HeaderText="Create Incident">
<ItemTemplate>
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="case#" HeaderText="Case #" SortExpression="case#" />
<asp:BoundField DataField="Unit #" HeaderText="Unit #" SortExpression="Unit #" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
<asp:BoundField DataField="typeOfCall" HeaderText="typeOfCall" SortExpression="typeOfCall" />
<asp:BoundField DataField="Primary Impression" HeaderText="Primary Impression" SortExpression="Primary Impression" />
<asp:TemplateField HeaderText="View PCR">
<ItemTemplate>
<asp:Button ID="ViewPCR" Text="View PCR" runat="server" OnClick="viewPCRClick" CssClass="btn em" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dismiss">
<ItemTemplate>
<asp:Button ID="dismiss" Text="Dismiss" runat="server" CommandName="Delete" CssClass="btn em" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my simple deletion function:
protected void dismissClick(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
GridView1.DeleteRow(index);
}
Calling GridView1.DeleteRow(index) is causing an event loop, because calling that method also raises the RowDeleted and RowDeleting events again, as described in the MSDN documentation for GridView.DeleteRow Method:
Calling this method also raises the RowDeleted and RowDeleting events.
Instead you want to either remove it from the binding source (i.e. DataSet, DataTable, List<T> or whatever you are binding to) or persist your delete to the database or whatever is ultimately holding your data and then re-bind the grid.
I have entered a value in the textbox, however when I clicked the button to get the value at server side, it still retrieved the old value.
Here is my code:
aspx:
<asp:GridView ID="gvTicketSkus" runat="server" AutoGenerateColumns="false"
CssClass="innergv" HeaderStyle-CssClass="innergvHeader" GridLines="Horizontal"
Width="570px">
<Columns>
<asp:BoundField HeaderText="Sku" DataField="TicketItemCode" />
<asp:BoundField HeaderText="TICKET TYPE" DataField="TicketItemCodeDescription" />
<asp:BoundField HeaderText="Tickets#" DataField="NumberOfWristbandsPerTicket" Visible="false" />
<asp:BoundField HeaderText="PRICE" DataField="Price" DataFormatString="{0:c}" />
<asp:TemplateField HeaderText="QUANTITY">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" TextMode="SingleLine" MaxLength="4" Width="80px" CssClass="quantity" Text="0"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#:
foreach (GridViewRow gvr in gvTicketSkus.Rows)
{
sku = gvr.Cells[0].Text;
quantity = Convert.ToInt32(((TextBox)gvr.FindControl("txtQuantity")).Text);
if (quantity > 0)
{
pendingOrder.Add(sku, quantity);
}
}
As you see when I get the value of txtQuantity, it gave me the old value, instead of the one I type in.
I guess there is some issue in postback.
You can try using
Request.Form[((TextBox)gvr.FindControl("txtQuantity")).ClientID];
To get exactly what was posted to the server.
You may need to work on the syntax. I can't test it right now.
I have a dropdownlist inside a detailsview where I will be editing records inside the details view. My problem is dropdownlist inside the details view is bind to a field and giving an error
'DropDownList1' has a SelectedValue which is invalid because it does
not exist in the list of items. Parameter name: value
This is due to the selectedvalue value is not in the dropdownlist.
Can someone tell me how to handle this error. I want to use try catch and do nothing and just ignore the error or maybe pass a blank value as selectedvalue in the dropdown.
Main problem I have is I am not sure where to put the try catch statement.
Complete code is below.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="RecordRef" DataSourceID="EntityDataSource1" Height="50px"
Width="125px">
<Fields>
<asp:BoundField DataField="RecordRef" HeaderText="RecordRef" ReadOnly="True"
SortExpression="RecordRef" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
SortExpression="CustomerID" />
<asp:TemplateField HeaderText="JobCategory" SortExpression="JobCategory">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="EntityDataSource2" DataTextField="ItemValue"
DataValueField="ItemValue" SelectedValue='<%# Bind("JobCategory") %>'>
</asp:DropDownList>
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ConnectionString="name=CRMSEntities" DefaultContainerName="CRMSEntities2"
EnableFlattening="False" EntitySetName="KeyValues">
</asp:EntityDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("JobCategory") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("JobCategory") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JobDescription" HeaderText="JobDescription"
SortExpression="JobDescription" />
<asp:BoundField DataField="JobDate" HeaderText="JobDate"
SortExpression="JobDate" />
<asp:BoundField DataField="JobStatus" HeaderText="JobStatus"
SortExpression="JobStatus" />
<asp:BoundField DataField="Referral" HeaderText="Referral"
SortExpression="Referral" />
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
You can handle exception with override OnRender method of your usercontrol with using try catch around base.Render
Rather than using Try ... Catch, you could use the following in code behind (VB in this case):
Dim ddlItem As ListItem
ddlItem = myDDL.Items.FindByValue(UserInput.ToString())
If Not ddlItem Is Nothing Then
ddlItem.Selected = True
Else
myDDL.Text = "Some text to indicate failure"
End If