How to make 1row to 2rows GridView in asp.net? - asp.net

I have GridView with 5 items like this.
I want to make this GridView as below big red square. How can I make 2rows in one GridView?
ASP Code :
<asp:GridView ID="GridView" runat="server" DataSourceID="GridView1"
OnRowCreated="GridView1_RowCreated" AutoGenerateColumns="false" EmptyDataText="표시할 내용없음">
<Columns>
<asp:BoundField DataField="Seq"/>
<asp:BoundField DataField="Title" />
<asp:BoundField DataField="UUSER" />
<asp:BoundField DataField="NoticeDate" />
<asp:BoundField DataField="SCnt" />
</Columns>
</asp:GridView>
C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
//e.Row.Cells[1].Attributes.Add("colspan", "2");
}

Related

how to dynamically add button in gridview cell

gridview cannot add an 'approve' button in gridview,can someone can help me thanks
http://i260.photobucket.com/albums/ii8/elvenchan2/rowAdd.png
behind code
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow dr in GridView2.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = new Button();
btn.Text = "Approve";
btn.ID = "Approve";
btn.Click += new EventHandler(Approve_Click);
if (dr.Cells[1].Text == "1")
{
dr.Cells[10].Controls.Add((Control)btn);
}
}
}
}
u must add a 10th cell at first
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow dr in GridView2.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = new Button();
btn.Text = "Approve";
btn.ID = "Approve";
btn.Click += new EventHandler(Approve_Click);
if (dr.Cells[1].Text == "1")
{
//Declare the bound field and allocate memory for the bound field.
ButtonField btnRent = new ButtonField();
//Initalize the DataField value.
btn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
btn.CommandName = "Button";
btn.ButtonType = ButtonType.Button;
btn.Text = "Rent";
btn.Visible = true;
//Add the newly created bound field to the GridView.
GridView2.Columns.Add(btn);
}
}
}
}
Since there is no criteria for creating the button, why not just make a TemplateField in your GridView markup and wire up a click handler or CommandName for the button, like this:
<asp:GridView id="GridView1" runat="server"
OnRowCommand="GridView1_RowCommand">
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="ButtonApprove" runat="server"
CommandName="approve" Text="Approve" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now in your code-behind, you can handle each individual approve button click, like this:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "approve")
{
// Do approval logic here
}
}
You can try it:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState != DataControlRowState.Edit)
{
PlaceHolder ph = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
Button b1 = new Button();
b1.ID = "Approve";
b1.Text = "Approve"
ph.Controls.Add(b1);
}
}
Of course, you should add a placeholder in the html template. like
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
Button add here..
</asp:PlaceHolder>
</ItemTemplate>
Design
<asp:GridView ID="viewThemeTypeAssociationsGridView" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2"
OnRowDataBound="viewThemeTypeAssociationsGridView_RowDataBound"
DataSourceID="Sql_New" >
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:TemplateField HeaderText ="New Column" >
<ItemTemplate >
<asp:Button ID="btnnew" runat ="server" Visible ="false" ></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="Sql_New" runat="server"
ConnectionString="<%$ ConnectionStrings:EmployeeConnectionString %>"
SelectCommand="SELECT * FROM [tblnew]"></asp:SqlDataSource>
Code
protected void viewThemeTypeAssociationsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[2].Text == " ")
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnnew = (Button)e.Row.FindControl("btnnew");
btnnew.Visible = true;
}
}
}

asp can't detect the checked item in datagridview

I have a gridview with ckeck column,my user can select the item via checked box so i add a checkbox column to my gridview:
<asp:GridView ID="taminkonandeh" runat="server" CssClass="gv-list-company-col-edit" ShowHeaderWhenEmpty="True" AutoGenerateColumns="False" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChTaminKonande" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="شماره " DataField="companyid" >
<HeaderStyle CssClass="gv-list-company-header" />
<ItemStyle CssClass="gv-list-company-col" />
</asp:BoundField>
<asp:BoundField HeaderText="نام شرکت " DataField="companyname">
<HeaderStyle CssClass="gv-list-company-header" />
<ItemStyle CssClass="gv-list-company-col" />
</asp:BoundField>
<asp:BoundField HeaderText="شماره ثبت " DataField="registrationNumber" >
<HeaderStyle CssClass="gv-list-company-header" />
<ItemStyle CssClass="gv-list-company-col" />
</asp:BoundField>
<asp:BoundField HeaderText="شماره اقتصادی" DataField="noEconomic" >
<HeaderStyle CssClass="gv-list-company-header" />
<ItemStyle CssClass="gv-list-company-col" />
</asp:BoundField>
</Columns>
<HeaderStyle CssClass="gv-list-company-header" />
<RowStyle CssClass="gv-list-company-row"/>
</asp:GridView>
and my code behind is :
for (int i = 0; i < taminkonandeh.Rows.Count; i++)
{
CheckBox chinvi = (CheckBox)taminkonandeh.Rows[i].FindControl("ChTaminKonande");
if (chinvi != null)
{
if (chinvi.Checked)
{
Count++;
comid = taminkonandeh.Rows[i].Cells[1].Text;
tblAnnouncePriceByCompany objanno = new tblAnnouncePriceByCompany();
objanno.letterId = objletter.letterId.ToString();
objanno.companyId = comid;
objanno.tenderId = Session["letterTenderId"].ToString();
db1.tblAnnouncePriceByCompanies.InsertOnSubmit(objanno);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "myalert",
"alert('تامین کننده ای انتخاب نشده است ')", true);
return;
}
}
but "chinvi.Checked" condition is always false and i can't detect which checkboxs are selected.
This situation happens when you bind the gridview in pageload, and each postback rebinds it, removes posted value like this:
protected void Page_Load(object sender, EventArgs e)
{
//Code to get datasource
taminkonandeh.DataSource = myDataSource;
taminkonandeh.DataBind();
}
You need to change it to :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Code to get datasource
taminkonandeh.DataSource = myDataSource;
taminkonandeh.DataBind();
}
}

How to call specific method when user click on row in GridView

I add below line to each row and when i click on row in GridView i can select it(means event GridView_SelectedIndexChanged hire) how can i change it that when i click on row specific method call
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
in view:
<script type="text/javascript" language="javascript">
function show(id) {
alert(id);
// Do something as your need
}
</script>
<asp:GridView runat="server" ID="GridView1" DataKeyNames="DepartmentID" AutoGenerateColumns="False"
Font-Names="Tahoma" Font-Size="Small"
OnRowDataBound="GridView1_RowDataBound" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID"
InsertVisible="False" ReadOnly="True" SortExpression="DepartmentID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Budget" HeaderText="Budget"
SortExpression="Budget" />
<asp:BoundField DataField="StartDate" HeaderText="StartDate"
SortExpression="StartDate" />
<asp:BoundField DataField="Administrator" HeaderText="Administrator"
SortExpression="Administrator" />
</Columns>
</asp:GridView>
inCodeBehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.cursor='pointer'");
e.Row.Attributes["onclick"] = "show(" + e.Row.RowIndex.ToString() + ");";
}
}
Use GridView.RowCommand Event: The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="YourCommandName")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = GridView1.Rows[index];
//Your Code
}
}
<asp:GridView runat="server" ID="GridView1" DataKeyNames="ID" AutoGenerateColumns="False"
Font-Names="Tahoma" Font-Size="Small" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Row">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//add css to GridViewrow based on rowState
e.Row.CssClass = e.Row.RowState.ToString();
//Add onclick attribute to select row.
e.Row.Attributes.Add("onclick", String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex));
}
}

How to diplay BoundField Columns values sum in Footer Row

I am binding a gridview, all columns are boundfields :
<asp:BoundField DataField="PLAN_SEP" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Plan For The Month" ReadOnly="True" SortExpression="PLAN_SEP" />
<asp:BoundField DataField="ACTUAL_SEP" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Actual For The Month" ReadOnly="True" SortExpression="ACTUAL_SEP" />
<asp:BoundField DataField="SCORE_FORMONTH" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Score" ReadOnly="True" SortExpression="SCORE_FORMONTH" />
<asp:BoundField DataField="PLAN_LIVE" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Plan Upto Month" ReadOnly="True" SortExpression="PLAN_LIVE" />
<asp:BoundField DataField="ACTUAL_LIVE" ItemStyle-Width="52px" ItemStyle-HorizontalAlign="Center" HeaderText="Actual Upto Month" ReadOnly="True" SortExpression="ACTUAL_LIVE" />
<asp:BoundField DataField="SCORE_UPTOMONTH" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Score" ReadOnly="True" SortExpression="SCORE_UPTOMONTH" />
Now I need to display a footer row which will show sums of respective column values.
Total | total 1 | total 2 | ......
How to achieve this ?
EDIT
For bound fields on ly
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
double total = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sNum = e.Row.Cells[1].Text;//just changed the index of cells based on your requirements
double sum;
if(double.TryParse(sNum,out sum))
{
total += sum;
}
GridView1.FooterRow.Cells[1].Text = total.ToString();
}
}
or
You can make use of databoudnc column and code like this
int total = 0;
protected void gvEmp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
//replace maounty with the name of property
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Amount"));
}
if(e.Row.RowType==DataControlRowType.Footer)
{
Label lblamount = (Label)e.Row.FindControl("lblTotal");
lblamount.Text = total.ToString();
}
}
Check turotiral : http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html
This can be easily done as follows:
In aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound" ShowFooter="true">
<Columns>
<asp:BoundField DataField="Qty" />
</Columns>
</asp:GridView>
In aspx.cs
//Define global variable
int totQty = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
totQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Qty"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = totQty.ToString();
}
}
That's it.

How can i call a code behind function when user clicks a gridview row?

I have a gridview in an aspx page with c# code behind.
Is there a way how i can run a code behind function when the user clicks anywhere on a row? For now, i use the select button. But then the user has to click on that button. And i want to user to be able to click anywhere on a row to show it's details in another gridview that is located next to the main gridview.
Some ideas here on how to do this?
Thanks!
try this
<asp:GridView runat="server" ID="GridView1" DataKeyNames="ID" AutoGenerateColumns="False"
Font-Names="Tahoma" Font-Size="Small" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Row">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = Enumerable.Range(1, 10).Select(a => new
{
ID = a,
FirstName = String.Format("First {0}", a),
LastName = String.Format("Last{0}", a)
});
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//Bind other grid
Response.Write(GridView1.SelectedIndex+1);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//add css to GridViewrow based on rowState
e.Row.CssClass = e.Row.RowState.ToString();
//Add onclick attribute to select row.
e.Row.Attributes.Add("onclick", String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex));
}
}

Resources