Difference between DataGrid and GridView

Hi All !

I am back !!!


Difference between DataGrid and GridView:
==========================================


In .Net 2.0, we are using GridView. Since DataGrid and GridView are same we have some extra facilities and features in GridView than DataGrid.

By using GridView we can render it on Mobile Devices also, but DataGrid only use for web pages. Another key difference between DataGrid and GridView controls lies in the adaptive user interface.

In DataGrid paging, sorting, inserting, updating and deleting are implementing by coding. But in GridView automatically generates those things without user writing the code.
Sorting: In DataGrid code requires handling the SortCommand event and rebind grid required. In case of GridView no additional code required.
Paging: In DataGrid requires code to handle the PageIndexChanged event and rebind grid required. In case of GridView no additional code required. It also supports customized appearance.
Data binding: Like GridView DataGrid cannot bind with new datasource control in ASP.NET 2.0.
Updating data: DataGrid requires extensive code to update operation on data. GridView requires little code. Code like exceptions handling for database part.
Events: GridView supports events fired before and after database updates. In DataGrid fewer events supported as compared to GridView.

In DataGrid no image template column, whereas GridView have image template column
The GridView control is the successor to the DataGrid control. Like the DataGrid control, the GridView control was designed to display data in an HTML table. When bound to a data source, the DataGrid and GridView controls each display a row from a DataSource as a row in an output table.

Both the DataGrid and GridView controls are derived from the WebControl class. Although it has a similar object model to that of the DataGrid control, the GridView control also has a number of new features and advantages over the DataGrid control, which include:

Richer design-time capabilities.

Improved data source binding capabilities.

Automatic handling of sorting, paging, updates, and deletes.

Additional column types and design-time column operations.

A Customized pager user interface (UI) with the PagerTemplate property.

Differences between the GridView control and the DataGrid control include:

Different custom-paging support.

Different event models.



...S.VinothkumaR.

How to clear temporary internet foder by using C# in winforms?

Here is the sample code for clearing temporary internet foder by using C# in winforms.

private void btnClear_Click(object sender, EventArgs e)
{
ClearAll(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
MessageBox.Show("Your temporary files has been cleaned.! ");
}

public void ClearAll(DirectoryInfo dirInfo)
{
foreach (FileInfo fileInfo in dirInfo.GetFiles())
{
try
{
fileInfo.Delete();
}
catch { }
}
foreach (DirectoryInfo subDirectory in dirInfo.GetDirectories())
{
ClearAll(subDirectory);
}
}


...S.VinotkumaR.

How to insert bulk values in to a table by using C#?

The below method is used to bulk insert in to a table by using c# from a datatable.

using System.Data;
using System.Data.SqlClient;

public int BulkInsert(DataTable dt, string TableName, string ConnectionString)
{
int returnValue = -1;
SqlConnection oConn = new SqlConnection(ConnectionString);
oConn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM " + TableName,oConn);
da.SelectCommand.Connection = oConn;
DataSet ds = new DataSet();
SqlCommandBuilder oCmd = new SqlCommandBuilder(da);
try
{
da.Fill(ds);
returnValue = da.Update(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oConn.Close();
oCmd.Dispose();
}
return returnValue;
}


...S.VinothkumaR.

Copy All Directories with Files using C#

Here is the sample code for copying a directory with sub directories and all the files using C#.


public static void CopyAllDirectories(DirectoryInfo source, DirectoryInfo target)
{
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}

foreach (FileInfo fileInfo in source.GetFiles())
{ fileInfo.CopyTo(Path.Combine(target.ToString(),fileInfo.Name), true);
}
foreach (DirectoryInfo sourceSubDir in source.GetDirectories())
{
DirectoryInfo targetSubDir =
target.CreateSubdirectory(sourceSubDir.Name);
CopyAllDirectories (sourceSubDir, targetSubDir);
}
}


...S.VinothkumaR.

Scrollbar in down side of textbox.

Scrollbar in down side of textbox.

Hi all, I have come to give a very simple solution for the below question. The question is

How to set scrollbar in end of the text in textbox when dynamically updating the text value?

Actually I have been developing a big application. On that application I needed to update the status of every action in my application. For the purpose of updating the status, I have used the textbox to show the status with multiline and vertical scrollbar properties. But I could see the vertical bar not going down when updating the status as shown in the below figure which is marked in red.




After googling few minutes I found we have some properties and methods in textboxes of visual studio are SelectionStart and ScrollToCaret method.

By using the above property and method I have found the solution for my question. See the below figure...



Code Snippet:

private void btnUpdate_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtStatus.Text))
txtStatus.Text = txtStatus.Text + Environment.NewLine;
txtStatus.Text = txtStatus.Text + "Status Updated...";

txtStatus.SelectionStart = txtStatus.Text.Length;
txtStatus.ScrollToCaret();
txtStatus.Refresh();
}


...S.VinothkumaR.

Barcode Generation using C#

In this article, I am trying to give you a sample code for generating barcode image by using C#.

We can see in market for all products they are using barcode only. So the barcode is common one. When we try to programming with old language for barcode is very difficult. We needed to go third party component. But in .Net framework it is very easy to generate barcode image.

Barcode font

There is lot of free barcode fonts available in internet. We can search and download it for generating the barcode image. Here is the font which is I am using “Barcodefont.ttf”. We need to download this font and install in your fonts folder which is located in control panel.

For generating barcode image, we need to add the namespaces “System.Drawing, System.Drawing.Imaging” and the file management namespaces.

Sample Code

using System.Drawing.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

public string dirPath = "";

dirPath = Environment.CurrentDirectory;

private void GenerateBarCode()
{
try
{
string path = dirPath + "\\" + DateTime.Now.ToFileTimeUtc().ToString() + ".jpg";
PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(dirPath + "\\BarcodeFont.ttf");
FontFamily fontFamily = new FontFamily("barcode font", fontCollection);
Font font = new Font(fontFamily, 40);

Bitmap bmp = new Bitmap(pbBarcodeImage.Width, pbBarcodeImage.Height);
Graphics graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
SizeF sizeF = graphics.MeasureString(txtEnter.Text.Trim(), font);
Brush brush = new SolidBrush(Color.Black);
graphics.DrawString(txtEnter.Text.Trim(), font, brush, 10, 10);
bmp.Save(path, ImageFormat.Jpeg);
bmp.Dispose();
pbBarcodeImage.Image = new Bitmap(path);
}
catch
{ }
}

Hence we are generating the barcode by using the above code.

...S.VinothkumaR.

Inserting Bulk Data in SQL Server from Text file

There is a simple query to do this bulk insert in to sql server from a text file. The thing is that we have to keep a fully formatted text file with appropriate data’s which are to be inserted.

Here, we have a table to be inserted is,

The query for creating table,

CREATE TABLE Employee
(
FName varchar (100) NOT NULL,
LName varchar (100) NOT NULL,
Email varchar (100) NOT NULL
)

Then we have a text file with the bulk data’s like as follows,

vinoth,kumar,itvinoth83@gmail.com
emp1FName,emp1LName,emp1@company.com
emp2FName,emp2LName,emp2@company.com
emp3FName,emp3LName,emp3@company.com
emp4FName,emp4LName,emp4@company.com
emp5FName,emp5LName,emp5@company.com
emp6FName,emp6LName,emp6@company.com
emp7FName,emp7LName,emp7@company.com
emp8FName,emp8LName,emp8@company.com
emp9FName,emp9LName,emp9@company.com
emp10FName,emp10LName,emp10@company.com
emp11FName,emp11LName,emp11@company.com
emp12FName,emp12LName,emp12@company.com

Now the query for inserting bulk data is,

BULK INSERT Employee FROM 'c:\bulktext.txt' WITH (FIELDTERMINATOR = ',')

Now you can see the data's inserted in table by the select query as follows,

select * from Employee

The following image is being explained the above matters.





...S.VinothkumaR.