Archive Page 2

Solving the encoding problem while writing to Response

Technology: C# ASP.NET

Level: Expert

Depth: Medium

I, once, had to present a save-as dialogue that would export the content of a GridView into csv and the database would contain Japanese texts. Export was all fine and the file was read fine from Visual Studio itself. However, when the csv was opened from Excel, the Japanese texts came all garbage.

This could have been for my PC doesn’t have Japanese locales and fonts. But the PC with such features also read it same. Here I present a way that solved this problem.

protected void ImageButton_CSV_Click(object sender, ImageClickEventArgs e)

{

MyDataSet.MyDataTable exportTable = …

HttpContext context = HttpContext.Current;

context.Response.Clear();

context.Response.ContentEncoding = Encoding.GetEncoding(“shift_JIS”);

for (int i = 1; i < exportTable.Columns.Count; i++)

{

context.Response.Write(exportTable.Columns[i].ColumnName + “,”);

}

context.Response.Write(Environment.NewLine);

string orig_string = “”;

byte[] unicodeBytes = null;

byte[] codBytes = null;

char[] codChars = null;

string codString = “”;

Encoding cod = Encoding.GetEncoding(“shift_JIS”);

Encoding unicode = Encoding.UTF8;

for (int i = 1; i < exportTable.Rows.Count; i++)

{

for (int j = 0; j < exportTable.Columns.Count; j++)

{

orig_string = exportTable[i][j].ToString().Replace(“,”, string.Empty).Replace(“&nbsp;”, string.Empty) + “,”;

unicodeBytes = unicode.GetBytes(orig_string);

codBytes = Encoding.Convert(unicode, cod, unicodeBytes);

codChars = new char[cod.GetCharCount(codBytes, 0, codBytes.Length)];

cod.GetChars(codBytes, 0, codBytes.Length, codChars, 0);

codString = new string(codChars);

context.Response.Write(codString);

}

context.Response.Write(Environment.NewLine);

}

context.Response.ContentType = “text/csv”;

context.Response.AppendHeader(“Content-Disposition”, “attachment; filename=my_file.csv”);

context.Response.End();

}

The codes in the bold do the actual trick. The content encoding and writing to the response in the correct manner is all that needs to be done.

This, however, is not limited to csv exports. You can try with other file formats too.

Cheers and Cheese (Y)

Locking Vertical Scrollbar in DataGridView or ListView WinForm C#

Technology: C# WinForm

Level: Expert

Depth: Medium

There are times that we display data in DataGridView or ListView. When the data flows beyond the size of the control, vertical scrollbar appears that we can use to navigate to positions of our choice. So good so far.

Now suppose that the data needs to be refreshed every 1 or 2 seconds. And we are viewing the bottom row or the bottom item, which means that the vertical scroll bar is at the bottom. Now, when the refresh method finishes execution, the data is repopulated and we notice that the vertical scroll bar jumps to the top. For constantly changing data with large number of rows or items, this poses a serious problem because the scrollbar is not locked to its position. Since the data refreshes very fast, dragging the scroll times 60 times a minute is also not plausible.

In this page, I put in my approach to counter the above problem. It’s simple and working.

The code goes like this:

public Form_ClosedPosition()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
dataGridView_Closed.Rows.Add(i.ToString(), DateTime.Now.ToString(“HH:mm:ss”), i.ToString());
}
ReloadList();
}
private void ReloadList()
{
this.SuspendLayout();
dataGridView_Closed.SuspendLayout();
for (int i = 0; i < 10; i++)
{
dataGridView_Closed.Rows.Add(i.ToString(), DateTime.Now.ToString(“HH:mm:ss”), i.ToString());
}
for (int i = 0; i < 10; i++)
{
dataGridView_Closed.Rows.RemoveAt(0);
}
dataGridView_Closed.ResumeLayout(false);
this.ResumeLayout(false);
}
private void Form_ClosedPosition_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
e.Cancel = true;
}
private void Form_ClosedPosition_VisibleChanged(object sender, EventArgs e)
{
//if (Visible == true)
//{
//    MessageBox.Show(“Timer started”);
//}
//else
//{
//    MessageBox.Show(“Timer stopped”);
//}
}
private void timer_Refresh_Tick(object sender, EventArgs e)
{
ReloadList();
}

public Form_ClosedPosition()

{

InitializeComponent();

for (int i = 0; i < 10; i++)

{

dataGridView_Closed.Rows.Add(i.ToString(), DateTime.Now.ToString(“HH:mm:ss”), i.ToString());

}

ReloadList();

}

private void ReloadList()

{

this.SuspendLayout();

dataGridView_Closed.SuspendLayout();

for (int i = 0; i < 10; i++)

{

dataGridView_Closed.Rows.Add(i.ToString(), DateTime.Now.ToString(“HH:mm:ss”), i.ToString());

}

for (int i = 0; i < 10; i++)

{

dataGridView_Closed.Rows.RemoveAt(0);

}

dataGridView_Closed.ResumeLayout(false);

this.ResumeLayout(false);

}

private void timer_Refresh_Tick(object sender, EventArgs e)

{

ReloadList();

}

What I am doing in the above code is appending the new rows and then removing the old rows from the beginning, instead of clearing the rows and then adding new rows. Doing the later, as we know, produce the flickering and jumping scroll bars as we have discussed intially.

The bottom line is: Don’t clear the rows and fill the control, instead, Append the new rows and remove the old ones.

Cheers and Cheese.

« Previous PageNext Page »