RowDataBound how to print multiple labels - asp.net

I am trying to calculate the date difference between two dates and want to show them in grid. I have created a table in the grid and the results can be printer there.
My RowDataBound is calculating everything but don't know how to print the results.
protected void grdADR_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblADRYear = (Label)e.Row.FindControl("lblADRYear");
Label lblBRCIDADR = (Label)e.Row.FindControl("lblBRCIDADR");
DataTable dt = new DataTable();
string strQuery = "Select ADR_TYPE,DatePART(dayofyear,Document_Date) AS DayOfTheYearADR,Document_Date From ADR Where BRCID = '" + lblBRCIDADR.Text.ToString() + "'"
+ " AND CAST(YEAR(Document_Date) AS VARCHAR(4)) = '" + lblADRYear.Text.ToString() + "' Order by DayOfTheYearADR";
dt = objAccess.GetDataTable(strQuery);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
int DayMarker = 0;
int LastDay = 0;
List<Label> labels = new List<Label>();
for (int rcnt = 0; rcnt < dt.Rows.Count; rcnt++)
{
DataRow dr = dt.Rows[rcnt];
Label lblADRTimeLineStart = new Label();
lblADRTimeLineStart.ForeColor = System.Drawing.Color.Red;
int RowDay = int.Parse(dr["DayOfTheYearADR"].ToString());
if (LastDay != RowDay)
{
Label lblEmptyBlock = new Label();
lblADRTimeLineStart.ForeColor = System.Drawing.Color.Blue;
lblEmptyBlock.Width = (RowDay - DayMarker) * 3;
labels.Add(lblEmptyBlock);
Label lblADRBlock = new Label();
lblADRTimeLineStart.ForeColor = System.Drawing.Color.Green;
lblADRBlock.Width = 3;
labels.Add(lblADRBlock);
DayMarker = RowDay + 3;
}
LastDay = RowDay;
}
}
}
}
}
ASPX File
<asp:GridView ID="grdADR" runat="server" DataSource='<%# functADRTimeLine(Eval("Month").ToString()) %>' AutoGenerateColumns="false" ShowHeader="true" AllowPaging="false" Width="1260px" GridLines="Both"
BorderWidth="0" BorderColor="#839168" BorderStyle="Solid" CellPadding="0" BackColor="white" OnRowDataBound="grdADR_RowDataBound">
<Columns>
<asp:TemplateField Visible="false">
<HeaderTemplate>BRCID</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblBRCIDADR" runat="server" Text='<%# Bind("BRCID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="0px" Wrap="true" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" HorizontalAlign="Center" />
<HeaderStyle BorderColor="black" BorderStyle="Outset" BorderWidth="1" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField Visible="false">
<HeaderTemplate>Year</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblADRYEAR" runat="server" Text='<%# Bind("ADR_YEAR") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="0px" Wrap="true" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" HorizontalAlign="Center" />
<HeaderStyle BorderColor="black" BorderStyle="Outset" BorderWidth="1" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="width: 150px">
<%--HERE I WANT TO PRINT MY RESULTS.--%>
</td>
<td style="width: 1101px"></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Height="25px" HorizontalAlign="Center" BackColor="#a8b194" />
<RowStyle CssClass="grd_RowStyle" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" Wrap="true" />
</asp:GridView>

There are many options for you to print the text
One option would be
Add a placeholder control in where you want the labels to be printed
<asp:PlaceHolder ID="placeHolderLabels" runat="server"></asp:PlaceHolder>
In the codebehind, find this placeholder like this
PlaceHolder placeHolderLabels= (PlaceHolder)e.Row.FindControl("placeHolderLabels");
Then add your labels to this placeholder control using the following code
placeHolderLabels.Controls.Add(lblEmptyBlock);
placeHolderLabels.Controls.Add(lblADRBlock);
Another option is, since you are printing table like structure, you can add a datalist/gridview/repeater control where you want to print the labels. Then you can bind your calculated list to this control, which is more cleaner

Related

How can I update all the data in a database specified by selected rows in Asp GridView with a button located outside of the GridView?

I looked for some similar questions, but found only ListView solutions
But I have a GridView and a button outside of the GridView and need to udate only records specified by a selected checkbox.
<asp:GridView ID="gvData"
runat="server" Width = "850px"
CellPadding="5"
AutoGenerateColumns="false"
AllowPaging ="true"
OnPageIndexChanging ="OnPaging" PageSize="5">
<HeaderStyle CssClass="HeaderStyle" />
<PagerStyle CssClass="HeaderStyle" HorizontalAlign="Center" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" />
<RowStyle CssClass="RowStyle" />
<RowStyle HorizontalAlign="Left" />
<Columns>
<asp:TemplateField HeaderText="Id" ItemStyle-Width="20px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select" ItemStyle-Width="20px">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked = "false"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Message" ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblMessage" runat="server" Width="500px" Text='<%# Eval("Message") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<div style="text-align:left;">
<asp:Button ID="btnChange" runat="server" Text="Change Type" OnClick="btnChange_Click" />
</div>
I have a method btnChange_Click():
protected void btnChange_Click()
{
int index = gvFailedMerchants.SelectedRow.RowIndex;
DbConnection.UpdateMessageType(index);
}
When I click the button, I'm getting the message
"Object reference not set to an instance of an object"
I want to write a logic that where I traverse the grid and add ids of the selected row into one string where all ids are separated by commas:
1,2,3,4,5
Then I will send this string to the stored procedure and use them in a query within IN close:
UPDATE MYTABLE SET column = myValue WHERE ID IN('1,2,3,4,5')
I do not know what is the right approach to the problem.
How can I do that?
Please try this,
<asp:TemplateField HeaderText="Select" ItemStyle-Width="20px">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked = "false"
OnCheckedChanged="ChkSelect_CheckedChanged" data-id='<%# Eval("id") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
protected void ChkSelect_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox chkSel = sender as CheckBox;
string id= chkSel.Attributes["data-id"].ToString();
if (chkSel.Checked == true)
{
Session["ID"] += (Session["ID"].ToString() == "" ? id: "," + id);
}
else
{
string existingCodes = Session["ID"].ToString();
Session["ID"] = existingCodes.Replace(id, "");
}
protected void btnChange_Click()
{
string ids = Session["ID"].ToString();
DbConnection.UpdateMessageType(ids);
}

delete selected rows from gridview on selecting option from a dropdownlist located outside the gridview

I have a dropdownlist and a gridview control. DropDownlist is outside the gridview.
Dropdownlist contains an option Delete.
Gridview contains checkbox so that rows can be selected.
here is the gridview code:
<asp:GridView ID="gvRefDetail" runat="server" CssClass="mGrid" AutoGenerateColumns="False"
PageSize="50" Font-Names="Segoe UI" Font-Size="10pt" Width="800" AllowPaging="true"
BackColor="White" BorderColor="Silver" EmptyDataText="No Record" BorderStyle="Double"
BorderWidth="1px" CellPadding="4">
<HeaderStyle BackColor="red" />
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="Checkbox2" type="checkbox" onclick="CheckAll(this)" runat="server" /></HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NameofReferred">
<ItemStyle Wrap="true" HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lbltext" runat="server" Text='<%#Bind("NameofReferred") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txttext" runat="server" Text='<%#Bind("NameofReferred")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerName">
<ItemStyle Wrap="true" HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lbltext" runat="server" Text='<%#Bind("CustomerName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txttext" runat="server" Text='<%#Bind("CustomerName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#CCFFFF" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Gray" BorderColor="Gray" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle Font-Bold="True" ForeColor="White" />
</asp:GridView>
c# code:
protected void ddlaction_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlaction.SelectedValue == "1")
{
GridViewRow gvrow = gvRefDetail.SelectedRow;
int id = Convert.ToInt32(gvrow.Cells[1].Text);
string delref = "delete from tbl_Refferal where ID='" + id + "' ";
con = new SqlConnection(conString);
con.Open();
SqlCommand cmd2 = new SqlCommand(delref, con);
int temp1 = cmd2.ExecuteNonQuery();
con.Close();
BindRef();
}
}
Now, my question is when I select a one or more rows from gridview and select the delete option from dropdownlist, the selected rows must get deleted.
How can I do this?
Code On ddlaction_SelectedIndexChanged and stringVariableContainCheckboxvalues is string variable fetch from gridview checkbox selected in format ('1,3,5,6')
if (ddlaction.SelectedValue == "1")
{
string delref = "delete from tbl_Refferal where ID in (#checkboxSelectedValues)";
con = new SqlConnection(conString);
SqlCommand cmd2 = new SqlCommand(delref, con);
cmd2.Parameters.AddWithValue("#checkboxSelectedValues", stringVariableContainCheckboxvalues);
con.Open();
int temp1 = cmd2.ExecuteNonQuery();
con.Close();
BindRef();
}
Fetch Multiple Checkbox values:
foreach (GridViewRow row in gvRefDetail.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb != null && cb.Checked)
{
// fetch values in string
}
}

one dataGrid with 3 search option

My scenario is : I have one dataGrid with 5-6 columns. i have 3 filters which use 3 SQL queries methods in codeBehind :SearchByName(), SearchByValue() , SearchByDate().I have fourth method show DefaultShow() which display all data without any filters.
Basicly I want when Page is loaded DefaultShow() method to load and then user can choose option for search with RadioButtonList
DefaultShow() code is :
public void DefaultShow()
{
string connectionString = cs.getConnection();
string query = "select Id ,name,value,Description,DateCreate from AllCostView where IdUser = '" + cui.getCurrentId() + "'";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand command = new SqlCommand(query, myConnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(command);
using (DataTable myDataTable = new DataTable())
{
mySqlAdapter.Fill(myDataTable);
GridViewCost.DataSource = myDataTable;
GridViewCost.DataBind();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
DefaultShow();
}
But this not work what am I miss?
My gridViewCode is this :
<div class ="gridView">
<asp:GridView ID="GridViewCost" runat="server" AutoGenerateColumns="False" CaptionAlign="Top"
ShowFooter="True" ShowHeaderWhenEmpty="True" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False"
AllowPaging="true"
PageSize="5"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" OnPageIndexChanging="GridViewCost_PageIndexChanging" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" >
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Номер" />
<asp:TemplateField HeaderText="Име">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>
<asp:BoundField DataField="Value" HeaderText="Стойност" />
<asp:BoundField DataField="Description" HeaderText="Описание" />
<asp:BoundField DataField="DateCreate" HeaderText="Дата" />
</Columns>
<AlternatingRowStyle />
<HeaderStyle HorizontalAlign="Right"/>
<PagerStyle />
<RowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" />
</asp:GridView>
If it's needed i will post more code . Thanks.

gridview delete happens but gridview doesnt refresh?

I've got a gridview inside an update panel.
In the gridview I have an image button.
The button is used to delete a row.
In the rowCommand event of the grid view I do something like this:
protected void gvLineItems_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
switch (e.CommandName)
{
case "Delete":
Label l = null;
l = (Label)row.FindControl("lblLineItemID");
if(l!=null)
{
long lID;
lID = Convert.ToInt64(l.Text);
BL.DeleteLineItem(Convert.ToInt64(hlID.Text), lID);
BindGrid(Session["SortExpression"].ToString(), Convert.ToInt32(rbSortGrid.SelectedValue));
}
break;
}
}
I debug this and I see the row deleted in the database (data is deleted correctly). But the gridview still shows the "deleted" row even after bind grid. Bind grid is simple it looks like this:
protected void BindGrid(string sortExpression, int sortDirection)
{
DataSet ds
ds = BL.GetLineItemGridData(Convert.ToInt64(hlID.Text), sortExpression, sortDirection);
gvLineItems.DataSource = ds.Tables[0];
gvLineItems.DataBind();
gvLineItems.Visible = true;
}
The dataset is returning the correct rows (without the deleted row) but when I look at the webpage it still shows the row that was deleted.
Edit
Someone asked for the HTML of the gridview, here it is:
<asp:UpdatePanel ID="myPanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView GridLines="Horizontal" CellPadding="4" Font-Size="Small"
DataKeyNames="ID" Width="100%" AlternatingRowStyle-BackColor="#e5f1fa"
BackColor="#E8E8E8" HeaderStyle-ForeColor="White"
HeaderStyle-BackColor="#1367AD" ID="gvLineItems" runat="server"
AllowSorting="True" AutoGenerateColumns="False" ShowFooter="True"
onrowcommand="gvLineItems_RowCommand" >
<Columns>
<asp:TemplateField>
<ItemStyle Width="1px" />
<ItemTemplate>
<asp:Label Width=".05px" ID="lblLineItemID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>' style="display:none"></asp:Label>
</ItemTemplate>
<ControlStyle Width="0px" />
<HeaderStyle Width="0px" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ToolTip="Select / Deselect all rows?" ID="HeaderLevelCheckBox" onclick="toggleSelection(this);" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelector" ToolTip="Select row?" runat="server" onclick="ChangeRowColor(this)" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibAddLineItem" runat="server" ImageUrl="images/InsertRow.gif" CommandName="Insert" ToolTip="Insert new line item."/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibDeleteLineItem" runat="server" ImageUrl="images/DeleteRow.gif" CommandName="Delete" ToolTip="Delete line item."/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item #" SortExpression="LineItemNumber">
<ItemTemplate>
<asp:Label runat="server" ID="lblItemNumber" Visible="True" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemNumber") %>' />
</ItemTemplate>
<ItemStyle Width="10%" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="LineItemQuantity">
<ItemTemplate>
<asp:TextBox Font-Names="Arial" ToolTip="Enter item quantity." ID="txtLineItemQuantity" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemQuantity") %>' />
</ItemTemplate>
<ItemStyle Width="4%" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="4%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="LineItemDescription">
<ItemTemplate>
<asp:TextBox Columns="15" Width="300px" Font-Names="Arial" ToolTip="Enter item description." ID="txtLineItemDescription" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemDescription") %>' />
</ItemTemplate>
<ItemStyle Width="15%" HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Added By" SortExpression="AddedBy">
<ItemTemplate>
<asp:Label runat="server" ID="lblAddedBy" Visible="True" Text='<%# DataBinder.Eval(Container, "DataItem.AddedBy") %>' />
</ItemTemplate>
<ItemStyle Width="10%" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Added On" SortExpression="AddedOn">
<ItemTemplate>
<asp:Label runat="server" ID="lblAddedOn" Visible="True" Text='<%# DataBinder.Eval(Container, "DataItem.AddedOn") %>' />
</ItemTemplate>
<ItemStyle Width="10%" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="10%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddLineItem" />
<asp:AsyncPostBackTrigger ControlID="rbSortGrid" />
</Triggers>
</asp:UpdatePanel>
Err I got it I just added this:
protected void gvLineItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataSet ds = null;
gvLineItems.DataSource = null;
ds = BL.GetLineItemGridData(Convert.ToInt64(hlID.Text), Session["SortExpression"].ToString(), Convert.ToInt32(rbSortGrid.SelectedValue));
gvLineItems.DataSource = ds.Tables[0];
gvLineItems.DataBind();
gvLineItems.Visible = true;
}
And removed the call to BindGrid from this:
protected void gvLineItems_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
switch (e.CommandName)
{
case "Delete":
Label l = null;
l = (Label)row.FindControl("lblLineItemID");
if(l!=null)
{
long lID;
lID = Convert.ToInt64(l.Text);
BL.DeleteLineItem(Convert.ToInt64(hlID.Text), lID);
//BindGrid(Session["SortExpression"].ToString(), Convert.ToInt32(rbSortGrid.SelectedValue));
}
break;
}
}
Alternatively you can bind your GridView in the Page PreRender event which occurs after processing of control events has been completed. You don't need to hook up the datasource and bind the grid in the rowCommand event then.

Query regarding the nested grid views in asp.net/C#

I have defined a nested grid view in the following way.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" GridLines="None">
<Columns>
<asp:BoundField DataField="Date Of Transaction" HeaderText="Date Of Transaction"
SortExpression="Date Of Transaction" />
<asp:BoundField DataField="Invoice Number" HeaderText="Invoice Number" SortExpression="Invoice Number" />
<asp:BoundField DataField="totalAmount" HeaderText="totalAmount" ReadOnly="True"
SortExpression="totalAmount" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="gridView2" runat="server" HorizontalAlign="Left" ShowHeader="false" GridLines="None" OnRowDataBound="gridView2_RowDataBound">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Button ID="Btn1" runat="server" Text="Download" OnClick="Btn1_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ComponentDBConnectionString %>"
SelectCommand="SelectUserPreviousHistory" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="XYZZ" Name="userName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The screenshot of the output is here. As you can see i have a "Download" button in each row of the child gridview (i.e., gridView2) but I want the download button to be last column but .net is rendering it to be the first column.
How can I do it?
More over gridview2 datasource is arraylist. Here is the code
gridView2.DataSource = titlesArrayList;
gridView2.DataBind();
Please help me
Thanks in anticipation
Why don't you simply add a Label before the Donwload-Button in the ItemTemplate? You could set the Label's Text in RowDataBound(gridView2_DataBound).
Edit: to show the header columns of the nested gridview in the header of the outer gridview, you could set ShowHeader="false" in the inner grid and use a HeaderTemplate with two labels for "Software Titles" and "Download here" and appropriate CSS-Styles to fit to the inner grid.
Edit:
Here is a working test-page. Pick the parts you didn't understand:
aspx:
<asp:GridView ID="GrdTransaction" runat="server" OnRowDataBound="GrdTransaction_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="DateOfTransaction" HeaderText="Date Of Transaction"
SortExpression="DateOfTransaction" />
<asp:TemplateField>
<HeaderTemplate>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><asp:Label ID="LblFileNameHeader" Text="File-Name" runat="server" /></td><td><asp:Label ID="LblDownloadHeader" Text="Download file" runat="server" /></td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:GridView ID="GrdDocument" runat="server" ShowHeader="false" GridLines="None" AutoGenerateColumns="false"
OnRowCommand="GrdDocument_RowCommand" OnRowDataBound="GrdDocument_RowDataBound">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="LblFileName" Text='<%# Eval("Doc")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Button ID="BtnDownload" runat="server" CommandArgument='<%# Eval("Doc")%>' CommandName="Download" Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Codebehind(converted from vb.net to c#):
public class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
this.GrdTransaction.DataSource = GetOuterGridSource();
this.GrdTransaction.DataBind();
}
}
private DataTable GetOuterGridSource()
{
DataTable tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID", typeof(Int32)));
tbl.Columns.Add(new DataColumn("DateOfTransaction", typeof(DateTime)));
DataRow row = tbl.NewRow();
row["ID"] = 1;
row["DateOfTransaction"] = System.DateTime.Now;
tbl.Rows.Add(row);
row = tbl.NewRow();
row["ID"] = 2;
row["DateOfTransaction"] = System.DateTime.Now;
tbl.Rows.Add(row);
row = tbl.NewRow();
row["ID"] = 2;
row["DateOfTransaction"] = System.DateTime.Now;
tbl.Rows.Add(row);
return tbl;
}
private DataTable GetNestedGridSource()
{
DataTable tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID", typeof(Int32)));
tbl.Columns.Add(new DataColumn("Doc", typeof(string)));
DataRow row = tbl.NewRow();
row["ID"] = 1;
row["Doc"] = "Smart Defrag";
tbl.Rows.Add(row);
row = tbl.NewRow();
row["ID"] = 2;
row["Doc"] = "Visio Viewer";
tbl.Rows.Add(row);
row = tbl.NewRow();
row["ID"] = 2;
row["Doc"] = "Rapid Typing";
tbl.Rows.Add(row);
return tbl;
}
protected void GrdTransaction_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
dynamic row = ((DataRowView)e.Row.DataItem).Row;
dynamic GrdDocument = (GridView)e.Row.FindControl("GrdDocument");
GrdDocument.DataSource = GetNestedGridSource();
GrdDocument.DataBind();
GrdDocument.RowCommand += GrdDocument_RowCommand;
}
}
protected void GrdDocument_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
dynamic row = ((DataRowView)e.Row.DataItem).Row;
dynamic LblFileName = (Label)e.Row.FindControl("LblFileName");
LblFileName.Text = row("Doc").ToString;
}
}
protected void GrdDocument_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Download") {
dynamic docName = e.CommandArgument.ToString();
}
}
public WebForm1()
{
Load += Page_Load;
}
}
I have set the LblFileName's Text poperty in GrdDocument_RowDataBound. That is redundant because i've always used eval on the aspx-page. I wanted to show both ways for the sake of completeness.
This is result:
in your gridView2, set AutoGenerateColumns="False" and add a asp:BoundField before the asp:TemplateField
Are you sure there is no missing code in your snippet?
<asp:GridView ID="gridView2" runat="server" HorizontalAlign="Left" ShowHeader="false" GridLines="None" OnRowDataBound="gridView2_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="" DataField="ToString" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Button ID="Btn1" runat="server" Text="Download" OnClick="Btn1_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Resources