1. Change the value of Gridview cell dynamically
if (frmid == "2")
{
dgdeadline.Rows[i].Cells[1].Text = "ASAP";
deadline = "-";
}
2. Conditional Values And Styles In GridView
-------------------------------------------------
Formatting data binding output using inline if condition
<asp:GridView ID="GridView1" runat="server">
 <Columns>
<asp:TemplateField>
<ItemTemplate>
<img src='<%# (Eval("ImageURL")==DBNull.Value ? "DefaultImage.jpg" : DataBinder.Eval(Container.DataItem, "ImageURL")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected string getImageURL(object OriginalImage)
{
// In protected function do needed processing to format or change
// output as needed
if(OriginalImage == null)
{
return "DefaultImage.jpg";
}
else
{
return (string)OriginalImage;
}
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img src='<%# getImageURL(Eval("ImageURL")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
3. Find Grid view id on select index change event
if (frmid == "2")
{
dgdeadline.Rows[i].Cells[1].Text = "ASAP";
deadline = "-";
}
2. Conditional Values And Styles In GridView
-------------------------------------------------
Formatting data binding output using inline if condition
<asp:GridView ID="GridView1" runat="server">
 <Columns>
<asp:TemplateField>
<ItemTemplate>
<img src='<%# (Eval("ImageURL")==DBNull.Value ? "DefaultImage.jpg" : DataBinder.Eval(Container.DataItem, "ImageURL")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected string getImageURL(object OriginalImage)
{
// In protected function do needed processing to format or change
// output as needed
if(OriginalImage == null)
{
return "DefaultImage.jpg";
}
else
{
return (string)OriginalImage;
}
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img src='<%# getImageURL(Eval("ImageURL")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
3. Find Grid view id on select index change event
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:Button ID="btnEdit" PostBackUrl="#A1" runat="server" Text="Edit" CausesValidation="False" CommandName="select"
Height="20px"
/>
</ItemTemplate>
</asp:TemplateField>
protected void
GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
lblMsg.Text = "";
txtFormid.ReadOnly = true;
Button1.Visible = false;
btnUpdate.Visible = true;
string selectquery = string.Empty;
selectquery = "Select
eventid,formid,formno,taskname,Form_msg,CONVERT(VARCHAR(12),Deadline,101)
Deadline,onlineform,email,standtype,CONVERT(VARCHAR(12),Live_date,101)
Live_date,status,date1,add1,add2 from tbl_Manual_Administrator_Panel where
eventid='" + Session["eventid"].ToString()
+ "' and formid='" +
GridView1.SelectedRow.Cells[0].Text + "'";
SqlCommand sqlc = new
SqlCommand(selectquery, Conn);
Conn.Open();
SqlDataReader drc = sqlc.ExecuteReader();
if (drc.Read())
{
drFormtype.Text = drc["Onlineform"].ToString();
drStandtype.Text = drc["StandType"].ToString();
txtEmail.Text = drc["Email"].ToString();
}
Conn.Close();
}
<a name="A1"></a>
4. Set Property
in grid view in code behind C#
GridView dgGrid = new
GridView();
dgGrid.HeaderStyle.BackColor =
System.Drawing.Color.Aqua;
dgGrid.BackColor = System.Drawing.Color.White;
dgGrid.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
dgGrid.RowStyle.HorizontalAlign = HorizontalAlign.Left;
dgGrid.RowStyle.VerticalAlign = VerticalAlign.Top;
dgGrid.DataSource = dt;
dgGrid.DataBind();
5. Add row
in grid view in run time with the help of row data bound event
if (e.Row.RowType == DataControlRowType.DataRow)
{
if
(frmid == "29")
{
//Create
new Row in Gridview
GridViewRow
row2 = new GridViewRow(-1,
-1, DataControlRowType.DataRow, DataControlRowState.Insert);
//create Cell
and it will be added to row
TableCell
cell2 = new TableCell();
cell.CssClass = "heading";
cell2.ColumnSpan = 5;
cell2.Text = "";
cell2.Width = new Unit("25px");
row2.Cells.Add(cell2);
//add
the row to Gridview and AddAT(Index of where it will be appear
cell2.BorderStyle = BorderStyle.None;
GridView3.Controls[0].Controls.AddAt(8, row2);
}
}
6. for each to
add attribute dynamically.
foreach (GridViewRow
row in ExtraBadgesGrid.Rows)
{
LinkButton
lb = (LinkButton)ExtraBadgesGrid.Rows[row.RowIndex].Cells[0].FindControl("lblDelete");
lb.Attributes.Add("onclick", "if(!confirm('The
badge willl be deleted permanently. Are you sure you wish to delete the
badge?'))return false;");
}
7. Way to find gridview id on any Button Click event or onload First bind the grid veiw
for (int i = 0; i < dgdeadline.Rows.Count; i++)
{
u can find value like this
string frmid = ds.Tables[0].Rows[i]["formid"].ToString();
Find any control
Label msg1 = (Label)dgdeadline.Rows[i].FindControl("Label1");
msg1.Visible = true;
HyperLink TaskName = (HyperLink)dgdeadline.Rows[i].FindControl("orderfrmname");
TaskName.NavigateUrl = "http://www.eventdata.co.uk/Exhibitor/SITS.aspx";
TaskName.Target = "_blank";
want to pass value mannually in control
if (frmid == "5")
{
dgdeadline.Rows[i].Cells[1].Text = "";
}
}
protected void dgdeadline_RowDataBound(object sender, GridViewRowEventArgs e)
{
//CODE for color of rows in gridview according their form status
// e.Row.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
// when u have taken bound filed in html page
<asp:BoundField DataField="formno" HeaderText="FORM NO" ItemStyle-BorderColor="#4A3292"
ItemStyle-BorderStyle="Solid" ItemStyle-BorderWidth="1px">
</asp:BoundField>
string frmid = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "formid"));
if (currentstatus == "Completed")
{
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#DAF0FF");
}
}
8. bind two column in gridview
<ItemTemplate>
<asp:Label ID="lj" runat="server" Text='<%#Eval("hallno")+ " , " + Eval("hallno")%>'></asp:Label>
</ItemTemplate>
//Gridview column formate for date in asp.net
<asp:BoundField DataField="ActivityDate" HeaderText="Activity Date" SortExpression="ActivityDate"
DataFormatString="{0:MM/dd/yyyy}" />
9. find row for drowdownlist
<asp:TemplateField HeaderText="Set Order" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblSetOrder" runat="server" Text='<%# Eval("spkregid")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddlSetOrder" runat="server" OnSelectedIndexChanged="OnSelectedIndexChangedddlSetOrder"
AppendDataBoundItems="true" AutoPostBack="true" Width="65px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
protected void OnSelectedIndexChangedddlSetOrder(object sender, EventArgs e)
{
//try
//{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
int Id = gvr.RowIndex;
DropDownList dropdownlist1 = (DropDownList)GridView1.Rows[Id].Cells[1].FindControl("ddlSetOrder");
//row number , row value
if ((Id + 1) < Convert.ToInt32(dropdownlist1.SelectedItem.Text.ToString()))
{
for (int i = Id; i < Convert.ToInt32(dropdownlist1.SelectedItem.Text.ToString()) - 1; i++)
{
Label lblOrder = (Label)GridView1.Rows[i + 1].Cells[1].FindControl("lblSetOrder");
UpdateOrder(Convert.ToInt32(i + 1), lblOrder.Text);
}
Label lblOrder1 = (Label)GridView1.Rows[Id].Cells[1].FindControl("lblSetOrder");
UpdateOrder(Convert.ToInt32(dropdownlist1.SelectedItem.Text.ToString()), lblOrder1.Text);
}
else
{
Label lblOrder1 = (Label)GridView1.Rows[Id].Cells[1].FindControl("lblSetOrder");
UpdateOrder(Convert.ToInt32(dropdownlist1.SelectedItem.Text.ToString()), lblOrder1.Text);
for (int i = Id; i > (Convert.ToInt32(dropdownlist1.SelectedItem.Text.ToString()) - 1); i--)
{
Label lblOrder = (Label)GridView1.Rows[i - 1].Cells[1].FindControl("lblSetOrder");
UpdateOrder(Convert.ToInt32(i + 1), lblOrder.Text);
}
}
bindgrid();
lblOutput.Text = "The order of images have been updated successfully.";
//}
//catch (Exception ex)
//{
//}
}
10. find id on row updating
protected void dgViewExUP_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string CatId = Convert.ToString(dgViewExUP.DataKeys[e.RowIndex]["CatID"]);
string FormId = Convert.ToString(dgViewExUP.DataKeys[e.RowIndex]["FormId"]);
//use dirrect get id from CatId
TextBox txt = (TextBox)dgViewExUP.Rows[e.RowIndex].Cells[0].FindControl("txtcatname");
//use txt.Text
}
11. get grid view id on load event.
string query = "select catname,ProdId,ProdCategory,Prod_Details,Prod_Code,Color,Prod_Description,Prod_EB_Rate,Prod_SP_Rate,Prod_Image,Prod_SP_date from tblProductMaster a join tblProductCategory b on a.ProdCategory=b.CatID and a.eventId=b.eventid and a.FormID=b.FormID where a.eventid='" + Session["eventid"] + "' and a.FormID='" + formid + "' and Prod_Status='Active' order by ProdId";
SqlDataAdapter da = new SqlDataAdapter(query, Conn);
DataTable ds1 = new DataTable();
da.Fill(ds1);
if (ds1.Rows.Count != 0)
{
grid_prod.DataSource = ds1;
grid_prod.DataBind();
grid_prod.Visible = true;
//Code for image link in gridview
string IsCatRepeat = string.Empty;
for (int i = 0; i < grid_prod.Rows.Count; i++)
{
Label lb = (Label)grid_prod.Rows[i].FindControl("lblCategory");
if (Convert.ToString(ds1.Rows[i]["catname"]) != IsCatRepeat)
lb.Text = Convert.ToString(ds1.Rows[i]["catname"]).ToUpper();
else
lb.Text = "";
IsCatRepeat = Convert.ToString(ds1.Rows[i]["catname"]);
// Code for images
Image img = (Image)grid_prod.Rows[i].FindControl("Image1");
if (!string.IsNullOrEmpty((Convert.ToString(ds1.Rows[i]["Prod_Image"]))))
img.ImageUrl = "document/" + Convert.ToString(ds1.Rows[i]["Prod_Image"]);
else
img.Visible = false;
}
12. grid view auto increment id
<%#Container.DisplayIndex+1 %>
No comments:
Post a Comment