Spliting data in Asp.Net Gridview - asp.net

I Have a set of choices(options) coming from Database(around 36 records) which I have to insert in Gridview in such a way that 18 will go one column and 18 in another. And As these are choices the columns are needs to be a check box columns. So What I did is I have created a data table with 2 columns and split the data accordingly and then bind it to the grid.But the problem is If the question list is having odd no of items count ie 37. It is adding a blank record with check box in my gridview. Your help will be appreciated...
see code below
In my Aspx.cs
DataTable dTable = new DataTable();
dTable.Columns.Add("Questionsclmn1", typeof(string));
dTable.Columns.Add("Questionsclmn2", typeof(string));
for (int item = 0; item < QuestionList.Count; item = item + 2)
{
DataRow drow = dTable.NewRow();
drow["Questionsclmn1"] = QuestionList[item].Question;
if ((item + 1) < QuestionList.Count)
drow["Questionsclmn2"] = QuestionList[item + 1].Question;
dTable.Rows.Add(drow);
}
GrdVwQuestionsList.DataSource = dTable;
GrdVwQuestionsList.DataBind();
In my Aspx file under gridview
<Columns>
<asp:TemplateField HeaderText="Please Choose the Options Below">
<ItemTemplate>
<asp:CheckBox ID="chkQuestionList1" runat="server"
Text='<%# DataBinder.Eval( Container.DataItem, "Questionsclmn1")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkQuestionList2" runat="server"
Text='<%# DataBinder.Eval( Container.DataItem, "Questionsclmn2")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Thanks in Advance.
Regards,
Chetan

I wouldn't use a GridView for this at all. CheckboxList can lay out checkboxes in two columns automatically.
<asp:CheckboxList runat="server" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Vertical" ... />
There's also DataList that supports the same properties but lets you use a template for the content.

Related

TreeList.SelectedItems showing only current page selected items in telerik

I am using Telerik Radtreelist controls here is the markup
<telerik:RadTreeList ID="rtreeList_Topic" runat="server" OnNeedDataSource="rtreeList_Topic_NeedDataSource"
ParentDataKeyNames="parent_topicid" DataKeyNames="topicid" AllowPaging="true" RenderMode="Classic" Skin="WebBlue"
AutoGenerateColumns="false" AllowSorting="true"
OnDeleteCommand="rtreeList_Topic_OnDeleteCommand" OnInsertCommand="rtreeList_Topic_OnInsertCommand"
OnUpdateCommand="rtreeList_Topic_OnUpdateCommand" AllowMultiItemSelection="True"
OnItemDataBound="rtreeList_Topic_OnItemDataBound" HeaderStyle-Height="35px">
<Columns>
<telerik:TreeListSelectColumn HeaderStyle-Width="25px">
</telerik:TreeListSelectColumn>
<telerik:TreeListTemplateColumn HeaderText="Topic ID" HeaderStyle-Width="40px">
<ItemTemplate>
<asp:HyperLink ID="targetControl" runat="server" NavigateUrl="#" Text='<%# Eval("topicid") %>'></asp:HyperLink>
</ItemTemplate>
</telerik:TreeListTemplateColumn>
<telerik:TreeListBoundColumn DataField="parent_topicid" UniqueName="parent_topicid" HeaderText="Parent Topic Id" Visible="False">
</telerik:TreeListBoundColumn>
Here, you can see I have a TreeListSelectColumn and after selection I want to get each selected TreeListDataItem..
for obvious reason I could do iterate over Radtreelistselected item
foreach(var item in rtreeList_Topic.SelectedItems)
{
var dr = dtselected.NewRow();
dr["parent_topic_id"] = item["parent_topicid"].Text.Contains(" ") ? DBNull.Value : (object) Convert.ToInt32(item["parent_topicid"].Text);
dr["subject_code"] = rcb_subject_code.SelectedItem.Text;
dr["topic_id"] = Convert.ToInt32(item["topicid"].Text);
dr["topic_description"] = item["topicname"].Text;
dr["topic_shortname"] = item["ShortName"].Text;
dtselected.Rows.Add(dr);
}
Now say on First page(in footer you can have pages) have selected 4 items and on second page I have selected 2 items.
so rtreeList_Topic.SelectedItems.Count = 6 ? but no it showing only second page's selected item.. why? If there is any problem in my code please let me know.. Thanks :)

Add dynamic radio button object to GridView

I need to add a custom radio button control that I created based on an if condition to my GridView. My radiobutton will be enabled or disabled based on this condition and will have the text
changed as well.
I'm trying to figure out how to add a radiobutton object into my data row instead of a string dt.Columns.Add("FirstName").
<telerik:RadGrid runat="server" ID="grd1" OnNeedDataSource="grd1_NeedDataSource">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridTemplateColumn HeaderText="Radiobutton header" UniqueName="col1">
<ItemTemplate>
<asp:RadioButton ID="rbType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "rbEnableorDisable")%>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="FirstName header" UniqueName="col2">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
CodeBehind
Private dt As DataTable
Private dr As DataRow
dt= New DataTable
dt.Columns.Add("rbEnableorDisable")
dt.Columns.Add("FirstName")
Dim rb As RadioButton
rb = New RadioButton
For each item in itemlist //some data iteration declared elsewhere
dr = dt.NewRow()
If (Condition)
rb.Text = "Should be Disabled"
rb.Enabled = False
Else
rb.Text = "Should be Enabled"
rb.Enabled = True
End if
dr.Item("FirstName") = item.FirstName
dr.Item("rbEnableOrDisable") = rb//?Code for inserting a radio button object
dt.Rows.Add(dr)
Next
With grd1
.DataSource = dt
.DataBind()
End With
So far with this code
I am only able to display the radiobutton text if i have dr.Item("rbEnableOrDisable") = rb.Text.
I need to display the whole radiobutton object(show the text and if it's enabled or disabled among others)
I tried
LocationData.Columns.Add(New DataColumn("rbType", GetType(RadioButton)))
but it seems I need to append to the ItemTemplate
Also tried adding the whole column dynamic with:
grd1.Controls.Add(rb)
You need to have something in your DataItem to put the radiobutton enabled or disabled and assigment to Enabled property.
Enabled='<%# DataBinder.Eval(Container.DataItem, "booleanData")%>'
If you need to put RadioButton checked or unchecked use Checked property.

How to read the data of the row when clicked the button in the same row

I have the data displayed in the grid as follows:
StartDate Enddate Button
16/3/2013 17/3/2013 Signup---> this is an ASP button
18/3/2013 19/3/2012 Signup----> this is an ASP button
20/3/2012 20/3/2012 Signup----> this is an ASP button
I want asp.net with c# code when i clicked on first row signup button i want to get the data of the first row. If clicked on second row i want only the data of the second row startdate and end time. How can i acheive this? Please hep me in this regard.
In the asp button, Set properties: CommandName="SignUp" CommandArgument='<%# Container.DataItemIndex%>'
Now, when this button is clicked it calls Gridview's RowCommand Event
In that event e.CommandArgument contains your Row Number.
GridViewRow gvr= gvLinkImages.Rows[e.CommandArgument];
Now you can use gvr.Cells[column number] to get the particular text (Not recommended)
or use findcontrol to get the label or literal of the start/end date( assuming you are using Templatefields)
Sample code below
C#
protected void gvResults_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SignUp")
{
int rwNumber = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = gvResults.Rows(rwNumber);
System.DateTime rowStartDate = default(System.DateTime);
System.DateTime rowEndDate = default(System.DateTime);
//If you are using Templatefields
Literal lblRowStartDate = gvr.FindControl("lblStartDate") as Literal;
Literal lblRowEndDate = gvr.FindControl("lblEndDate") as Literal;
rowStartDate = Convert.ToDateTime(lblRowStartDate.Text);
rowEndDate = Convert.ToDateTime(lblRowEndDate.Text);
//Incase you are not using TemplateFields or Autobinding your grid
rowStartDate = Convert.ToDateTime(gvr.Cells[0].Text);
rowEndDate = Convert.ToDateTime(gvr.Cells[1].Text);
}
}
ASPX
<asp:GridView runat="server" ID="gvResults" AutoGenerateColumns="false" OnRowCommand="gvResults_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal runat="server" ID="lblStartDate" Text='<%#Container.DataItem.StartDate%>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal runat="server" ID="lblStartDate" Text='<%#Container.DataItem.EndDate%>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btnSignUp" CommandName="SignUp" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Binding data to Gridview Header Template? In Asp.net

In web application, I am trying to bind the Header Template of the gridivew, but i am not able to bind the data to gridview header.
<asp:GridView ID ="grdInner" runat ="server" AutoGenerateColumns ="false" >
<Columns >
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="lblHeader" runat="server" Text='<%# Eval("title") %>'></asp:Label>
</HeaderTemplate>
<ItemTemplate >
<asp:Label ID ="lblDesc" runat ="server" Text ='<%# Eval("description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Following code achieves the same things
<asp:GridView runat="server" ID="gridView" onrowdatabound="gridView_OnRowDataBound" AutoGenerateColumns="false">
<columns>
<asp:TemplateField>
<HeaderTemplate><asp:Label runat="server" ID="lblHeader"></asp:Label></HeaderTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
You can set label text in OnRowDataBound event
protected void gridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
((Label)e.Row.FindControl("lblHeader") as Label).Text = "Your Data here";
}
Another way to do this that is fairly simple if you want to bind the DataTable column headers to the Gridview headers (for example, you are generating DataTables where the column names can be different and you want these dynamic names to be the GridView header column names).
DataTable dt = // Call your DataTable here.
// Get your column names here.
string nutrient = dt.Columns[2].ColumnName;
string nutrient1 = dt.Columns[3].ColumnName;
string nutrient2 = dt.Columns[4].ColumnName;
string nutrient3 = dt.Columns[5].ColumnName;
string nutrient4 = dt.Columns[6].ColumnName;
string nutrient5 = dt.Columns[7].ColumnName;
GridView1.DataSource = dt;
GridView1.DataBind();
if (GridView1.Rows.Count > 0)
{
GridView1.HeaderRow.Cells[2].Text = nutrient;
GridView1.HeaderRow.Cells[3].Text = nutrient1;
GridView1.HeaderRow.Cells[4].Text = nutrient2;
GridView1.HeaderRow.Cells[5].Text = nutrient3;
GridView1.HeaderRow.Cells[6].Text = nutrient4;
GridView1.HeaderRow.Cells[7].Text = nutrient5;
}
You can also do the same thing with the footer row using GridView1.FooterRow.Cells[x].Text = ...

GridView Column Width Altering

Can someone please help me with my problem?
I have a gridview in a webpage, whose datasource is an oracle database. I would like to know if it is possible to change the column widths while you are on the web page?
string select = "SELECT * FROM TABLE"
OracleCommand cmd = new OracleCommand(select, connectionstring);
cmd.Connection.Open();
GridView.DataSource = cmd.ExecuteReader();
GridView.DataBind();
This outputs the table in the gridview. Is there a way to change gridview properties while in the browser? perhaps some javascript?
Thank you in advance,
You can specify these attributes through the styles in the columns of the gridview itself. You can set the AutoGenerateColumns attribute to false, and specify the columns you want, and specify the sizes within each column. Furthermore, following good web-design practices, you can use css to style each column. If you would like to change it on the fly, I would suggest using ItemTemplates, and adding components you can modify in the field.
Ex:
<asp:GridView runat="server" AutoGenerateColumns="false" ID="Grid" DataSourceID='MyObjectDataSource' CssClass="grid_table">
<AlternatingRowStyle CssClass="even" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div id="NameColumn" runat="server">Name Center</div>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("LastName") %>, <%# Eval("FirstName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<div id="AgeColumn" runat="server">Age</div>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Age") %>
</ItemTemplate>
</asp:TemplateField>
<EmptyDataTemplate>There are no Users</EmptyDataTemplate>
</asp:GridView>
With this code you can access the column widths:
NameColumn.Width = 100;
AgeColumn.Width = 2;
However, there are many ways you can accomplish this, including getting the columns directly:
Grid.Columns[0].HeaderStyle.Width = 100;
Grid.Columns[0].ItemStyle.Width = 100;
Grid.Columns[1].HeaderStyle.Width = 2;
Grid.Columns[1].ItemStyle.Width = 2;
Or again using css:
Grid.Columns[0].HeaderStyle.CssClass = "name_column_header";
Grid.Columns[0].ItemStyle.CssClass = "name_column_data";
Grid.Columns[1].HeaderStyle.CssClass = "age_column_header";
Grid.Columns[1].ItemStyle.Width = "age_column_data";
Anyways, the world's your oyster, these are just some starting points.
Use this code in row databound event of the gridview
e.Row.Cells[8].Width = new Unit("800px");
please ensure your gridview size must be greater than the column size.

Resources