BLOB data for simple row in DataGridView?

I am using C # and MYSQL to develop a desktop application.

In one of my forms I have DataGridView

(dgvBookings) and my database table has a tblBookings table that contains a specialization

type field BLOB

.

I am fetching data with the following query,

SELECT * FROM tblBookings WHERE IsActive=1;

      

and then data binding to DataGridView like,

dgvBookings.DataSource = ds.Tables["allbookings"];

      

BUT after data binding in gridview, it shows the value Byte[] Array

for all rows of the column specialization

which is BLOB type.

How to solve this problem, I want the data in String format, everything written in this column should be displayed as it is in the grid.

+2


source to share


3 answers


Split event CellFormatting

and String

cast byte array by or cast BLOB to character type in your SQL query.



+1


source


You will need to convert your byte array to string before using the DataSet:


DataTable allBookings = ds.Tables["allbookings"];
DataColumn column = allBookings.Columns.Add("NotABlobAnyMore", typeof(string));

foreach (DataRow row in allBookings.Rows) {
    row[column] = Encoding.Unicode.GetString((byte[])row["specialization"]);
}

dgvBookings.DataSource = allBookings ;

      



I'm using Unicode in the sample, but you must use whatever encoding you use to generate a byte array (blob).

If you are going to allow updates on the new column, you will need to convert the string back to a byte array (in the old column) before sending the data back to the database.

+4


source


Well, the inherent problem with BLOBs is that it ... is not text. It is binary. It could be an image (jpg, etc.), Exe, or it could be text ... but which encoding? UTF8? Not easy. If it was a CLOB I would hope it would work fine, but otherwise I would fully expect you to have to parse the BLOB manually, otherwise how would it be interpreted?

For any BLOB, the closest I can think of is to just show something like hex ... but with DataTable

even that call. You can do this for a class via TypeConverter

...

+1


source







All Articles