Showing posts with label DataGridView. Show all posts
Showing posts with label DataGridView. Show all posts

Saturday, October 10, 2009

ASP.NET - Image handler to fetch and show Images from varbinary(MAX)

Imagine you want to get images from [Production].[ProductProductPhoto] table in AdventureWorks SQL sample database. The following code can be in a generic handler (.ashx) file.

public class ImgHandler : IHttpHandler
{
private string sqlCnStr =
   ConfigurationManager.ConnectionStrings["CnStrAdvWrk"].ConnectionString;

public void ProcessRequest(HttpContext context)
{
   string productId = context.Request.QueryString["PrdId"];
   if (string.IsNullOrEmpty(productId))
      productId = "1";

   using (SqlConnection sqlCn = new SqlConnection(sqlCnStr))
   {
   StringBuilder cmd = new StringBuilder();
   cmd.Append("SELECT PrdPhoto.[ThumbNailPhoto] as PrdPhoto ");
   cmd.Append(" FROM [Production].[Product] as Prd ");
   cmd.Append(" JOIN [Production].[ProductProductPhoto] as PPrdPhoto ");
   cmd.Append(" ON Prd.ProductID = PrdPrdPhoto.ProductID ");
   cmd.Append(" JOIN [Production].[ProductPhoto] as PPhoto ");
   cmd.Append(" ON PPrdPhoto.ProductPhotoID = PPhoto.ProductPhotoID ");
   cmd.Append(" WHERE Prd.[ProductID] = " + productId);

   sqlCn.Open();
   SqlCommand sqlCmd = new SqlCommand(cmd.ToString(), sqlCn);
   SqlDataReader sqlDtaRdr = sqlCmd.ExecuteReader();
   sqlDtaRdr.Read();
   context.Response.BinaryWrite( (byte[]) sqlDtaRdr[0] );
   sqlCn.Close();
   context.Response.End();
   }
}

   public bool IsReusable
   {
   get { return false; }
   }
}


Now you just need to call the handler.

If you have an image control and you know the ProductId:

<asp:Image ID="Image1" runat="server"
ImageUrl="~/ImgHandler.ashx?PrdId=1" />


or if you have a GridView and you want to have a column with product images, then you can use TemplateField with and Image control inside the ItemTemplate:

<asp:TemplateField HeaderText="Image">
   <ItemTemplate>
      <asp:Image ID="Image1" runat="server"
      ImageUrl='<%# "ImgHandler.ashx?PrdId=" + Eval("PrdId") %>' />'
   </ItemTemplate>
</asp:TemplateField>

Share/Bookmark

Tuesday, October 6, 2009

ASP.NET - Do you need a GridView with scroll bars?

If you are looking for a GridView control that scrolls inside a web page, well, it's simple.
- Add a panel control
- Place the GridView control inside the Panel
- Set the panel size
- Set the Panel.Scrollbars property to Vertical, Auto, or Both!
Share/Bookmark

Tuesday, April 21, 2009

C# - A custom DataGridView class with always vertical scroll bar visible

Depend on the data which you want to show in a DataGridView, sometimes you need to keep vertical scroll bars always visible. If you don't have enough row to fill the DataGridView vertically, and you aligned and configured the columns' width for situations which data show with vertical scroll bar, then it's better to show the bar always.

The only thing which you need to do the job, is to set the VerticalScrollBar.Visible property to "true" in VisibleChanged event. I added dgvSetVerticalBar to fix the position of scroll bar, and it's necessary to call it in Load event of form which contains this custom DataGridView.


using System;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;

namespace myNameSpace
{
public partial class myCustomDGV : DataGridView
{
public myCustomDGV()
{
// InitializeComponent();
this.VerticalScrollBar.Visible = true;
this.VerticalScrollBar.VisibleChanged +=
new EventHandler(VerticalScrollBar_VisibleChanged);

this.AllowUserToOrderColumns = false;
this.AllowUserToResizeColumns = false;
this.AllowUserToResizeRows = false;
this.RowHeadersVisible = false;
}

void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
{
this.VerticalScrollBar.Visible = true;
}

public void dgvSetVerticalBar()
{
this.VerticalScrollBar.SetBounds(
this.Width - this.VerticalScrollBar.Width - 1,
this.VerticalScrollBar.Location.Y + 1,
this.VerticalScrollBar.Width,
this.Height - 2);
}

}
}

Share/Bookmark

Monday, April 20, 2009

C# - DataGridViewColumn.SortMode

.SortMode Property is related to the sort mode for the column in DataGridView control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.sortmode.aspx

....
Share/Bookmark

Sunday, April 19, 2009

C# - How to bind a TextBox to a DataGridView column

The goal is "bind a TextBox to a column in a DataGridView control" to be able to edit the cell in that columns for each rows and also show the value of the cell in that column when user move through the grid rows.

In the following code, imagine that we have a DataGridView called dgvTst and a TextBox control that called txtTst. To bind, CellClick event of DataGridView is coded as following:

private void dgvTst_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (txtTst.DataBindings.Count > 0)
txtTst.DataBindings.RemoveAt(0);

// The code binds column index 2 to the TextBox control
txtTst.DataBindings.Add(
new Binding("Text", dgvTst[2, e.RowIndex], "Value", false) );
}


Share/Bookmark

Monday, March 2, 2009

C# - DataGridView - delete a row by context menu strip

"How to remove a data row from a DataGridView control using a context menu strip"

The following codes just show one of possible methods to do the job.

To keep the DataGridView row index which is selected by right click, we need a private variable in our form class, let's call it iDGVrow2Del. You need to drag and drop a context menu strip control to your form that we call it mnuStrp4DgvMyData too for our example. Then you should show the menu when user right click on a row in your DataGridView which we call it dgvMyData here. To find when the user does right click, you can write your code in CellMouseUp method of DataGridView and if you found a right click, just you need to keep the row index and then Show the context menu. The menu can keep just an item with "Delete Row" as its text. At last code for Click event of that menu strip object to check if the row is not an empty new row in your DataGridView then remove it using its index in iDGVrow2Del variable from your Row collection of the DataGridView. Here you go ... The simple sample code is as following:


private int iDGVrow2Del = 0;

private void dgvMyData_CellMouseUp(object sender,
DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.dgvMyData.Rows[e.RowIndex].Selected = true;
this.iDGVrow2Del = e.RowIndex;

this.dgvMyData.CurrentCell =
this.dgvMyData.Rows[e.RowIndex].Cells[1];

this.mnuStrp4DgvMyData.Show(this.dgvMyData, e.Location);
}
}

private void mnuStrp4DgvMyData_Click(object sender, EventArgs e)
{
if (!this.dgvMyData.Rows[this.iDGVrow2Del].IsNewRow)
this.dgvBcktSrch.Rows.RemoveAt(this.iDGVrow2Del);
}

Share/Bookmark

Monday, September 22, 2008

How to limit the number of rows to enter in DataGridView

In a DataGridView if the number of rows should be limited to a
predefined value, we can switch AllowUserToAddRows to "true"
or "false", it will let the user be able to add new rows or stop
the user for entering more new rows.

RowLeave event is the place to count number of Rows and if it's
equal to limit, then we need to stop adding more. on the other hand
if user delete some row(s), it opens new room for adding new ones.

In the following part of code I considered to have just 20 rows in
our DataGridView control:


private void dgv1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
  if (dgv1.Rows.Count >= 20)
      dgv1.AllowUserToAddRows = false;
}
private void dgv1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
  if (dgv1.Rows.Count < 20)
      dgvTmp.AllowUserToAddRows = true;
}

Share/Bookmark