Beruflich Dokumente
Kultur Dokumente
This sample code shows how to create a ThumbNail image of width and Height 250 X
250 and save the Thumbnail Image in the Images folder of the Application.
private void SaveThumbNail (string folder, string filename)
{
System.Drawing.Image image =
System.Drawing.Image.FromFile(folder + filename);
System.Drawing.Image thumbnailImage =
image.GetThumbnailImage(250, 250, new
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);
string thumbnailPath = folder + "\Thumbnail.JPG";
thumbnailImage.Save(thumbnailPath,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
///
/// Required, but not used
///
/// true
public bool ThumbnailCallback()
{
return true;
}
int tdcount = 0;
//get the count of table cells in tr
tdcount = tr1.Controls.Count;
//Create a new table cell
HtmlTableCell tdcell = new HtmlTableCell();
//Create a new image control
Image img = new Image();
//set the url for image
img.ImageUrl = "C:\img.jpg";
img.Width = 120; //set the image width
img.Height = 100; //set the image hight
tdcell.ColSpan = 3; //set the colspan for table cell
tdcell.RowSpan = 4; //set the rowspan for table cell
tdcell.Controls.Add(img); // place image in table cell
tr1.Controls.AddAt(tdcount, tdcell); // Add table cell to the
table row.
The code behind page handles the event that populates data in GridView
sh.get_username = tx1.Text;
sh.get_location = tx2.Text;
sh.get_contactno = tx3.Text;
sh.update_search();
GridView1.EditIndex = -1;
bindall();
}
protected void GridView1_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;
sh.get_userid = id;
sh.delete_search();
bindall();
}
}
</asp:GridView>
</div>
</form>
The following code is the code-behind C# file for the above .aspx page.
//Code for binding the GridView
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class NestedGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
try
{
SqlConnection conn = new SqlConnection("");
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter ADA = new SqlDataAdapter("", conn);
ADA.Fill(dt);
Grd.DataSource = dt;
Grd.DataBind();
}
catch (Exception E)
{
string str = E.Message;
}
}
protected void OnDataBound(object sender , GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView Grd = (GridView)e.Row.FindControl("ChldGrid");
DataTable dt = new DataTable("TEST");
dt.Columns.Add("agent_cd");
dt.Columns.Add("agent_name");
dt.Columns.Add("address1");
}
}
dt.Columns.Add("city");
dt.Columns.Add("state");
dt.ReadXml(Server.MapPath("Data.xml"));
Grd.DataSource = dt;
Grd.DataBind();
Give below is a sample XML Content that can be used to test the program. Save this
content as a .xml file.
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<TEST>
<agent_cd>00000026</agent_cd>
<agent_name>TROPIC ICE INCORPORATED</agent_name>
<address1>2805 N COMMERCE PKWY</address1>
<city>MIRAMAR</city>
<state>FL</state>
</TEST>
</DocumentElement>
<ItemTemplate >
<asp:LinkButton ID="lnkDownload"
ForeColor="DarkOrange" runat="server" CausesValidation="False"
CommandName="Select"
Text="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</form>
50);
50);
cmdUploadFile.Parameters[0].Value = strFileName;
cmdUploadFile.Parameters[1].Value = bytFile;
cmdUploadFile.Parameters[2].Value = strFileType;
sqlConn.Open();
cmdUploadFile.ExecuteNonQuery();
sqlConn.Close();
Fill();
}
protected void gvFiles_SelectedIndexChanged(object sender, EventArgs
e)
}
sqlConn.Close();
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = GetSortExpression(e.SortExpression);
grid.DataSource = dataView;
grid.DataBind();
}
}
private string GetSortExpression(string sortExpression)
{
if (sortExpression == GridViewSortExpression && GridViewSortDirection
== "ASC")
{
GridViewSortDirection = "DESC";
}
else
{
GridViewSortDirection = "ASC";
}
GridViewSortExpression = sortExpression;
return sortExpression + " " + GridViewSortDirection;
OnPageIndexChanging="grcoll_PageIndexChanging"
PagerStyle-HorizontalAlign="Center" OnSorting="grcoll_Sorting"
Columns
asp:BoundField DataField="OrderId" SortExpression="OrderId" HeaderText="Order Id"
HeaderStyle-ForeColor="#78776b"
asp:BoundField DataField="OrderDate" SortExpression="OrderDate"
HeaderText="Order Date" HeaderStyle-ForeColor="#78776b"
asp:BoundField DataField="ShippedDate" SortExpression="ShippedDate"
HeaderText="Shipped Date" HeaderStyle-ForeColor="#78776b"
asp:TemplateField HeaderText="Action"
ItemTemplate
asp:LinkButton CssClass="links" ID="lnkView" runat="server" Text="View"
CommandName="viewOrder" CommandArgument='<%#Eval("OrderId") %>'
/ItemTemplate
/asp:TemplateField
/Columns
/asp:GridView
bindGrid("OrderId", sort);
//bindGrid();
bindCutomerDetail();
}
}
}
protected void bindCutomerDetail()
{
try
{
string sql = "select * from Customers where customerId='" +
Request["custId"].ToString() + "'";
SqlDataReader dr = DBAccess.ExecuteReader(m_ConnectionString,
CommandType.Text, sql);
if (dr.HasRows)
{
dr.Read();
lblAddress.InnerHtml = dr["address"].ToString();
lblax.InnerHtml = dr["fax"].ToString();
lblCity.InnerHtml = dr["city"].ToString();
lblCompanyName.InnerHtml = dr["CompanyName"].ToString();
lblContactName.InnerHtml = dr["ContactName"].ToString();
lblContactTitle.InnerHtml = dr["ContactTitle"].ToString();
lblCountry.InnerHtml = dr["Country"].ToString();
lblPhone.InnerHtml = dr["phone"].ToString();
lblPostalCode.InnerHtml = dr["postalCode"].ToString();
lblRegion.InnerHtml = dr["region"].ToString();
}
dr.Dispose();
dr.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
protected void bindGrid(string column, string expression)
{
lblcustomerId.Text = Request["custId"].ToString();
string sql = "select orderId,orderDate,shippedDate from Orders where CustomerId='" +
Request["custId"].ToString() + "'";
DataSet ds = DBAccess.ExecuteDataset(m_ConnectionString, CommandType.Text, sql);
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = column + " " + expression;
grcoll.DataSource = dv;
grcoll.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
}
else
{
lblmsg2.Text = "No records Found";
}
}
protected void grcoll_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
}
}
}
protected void grcoll_Sorting(object sender, GridViewSortEventArgs e)
{
if (sort == "asc")
{
bindGrid(e.SortExpression, "desc");
sort = "desc";
}
else
{
bindGrid(e.SortExpression, "asc");
sort = "asc";
}
}
}
How to get a seelcted row from gridview on button click uisng row
command event in c#
This small code will help some body for fetch the data from different controls in a
gridview.
I have created a gridview and bind it with 3 data bound column(id,name and age) from
sql datasource . i have added a button field (its not a template column) with button type as
last column .so when i click the button aganist on each row the other column values on
corrosponding row should be fetched.
I have Mapped the follwing procedure (MyGridview_RowCommand)to RowCommand
Event through property box of grid view.
For button field (last column of gridview)i have given the name "FindValue" as
CommandName for conditional running of code at runtime.
protected void MyGridview_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName.ToString() == "FindValue")
{
GridViewRow row =
((GridView)e.CommandSource).Rows[Convert.ToInt32(e.CommandArgument)];
string id = ((Label)(row.FindControl("label1"))).Text;
string name = ((Label)(row.FindControl("Label2"))).Text;
}
}