Save grid data to byte array()

Hi,

I would like to save the contents of the grid into a byte array, memorystream(), or something which I can create a csv, then upload to the server.

I do not want any save prompt, when the user has enetered the data they will click a .net button to save, which converts to .csv or a byte array(), memorystream(). Is this possible?

Hi,

Thank you for considering Aspose.

You can use WebWorksheets.SaveCSVFile(MemoryStream) method to save the worksheets in a memory stream to be written as CSV file. Please see the following sample code which will help you get your desired results,
Sample Code:

MemoryStream mstream = new MemoryStream();

GridWeb1.WebWorksheets.SaveCSVFile(mstream);

mstream.Flush();

// Writes the content of the memory stream to a file.

FileStream fout = new FileStream("c:\\out.csv", FileMode.Create);

fout.Write(mstream.GetBuffer(), 0, System.Convert.ToInt32(mstream.Length));

fout.Close();

Thank You & Best Regards,

Thank you for the reply, works great. however I have noticed an issue with saving the data in the format entered of the cells.

For example, if I enter a number in the format 00000784647098, when I click on the 'tick' to save the changes, it removes the leading zeros. I need this column to force numbers to be treated as text.

Also is there a function I can call to save the data on the grid without having to click the 'tick'?

Hi,

Thank you for considering Aspose.

You can use the cell.NumberType = NumberType.Text to set the values in the Cells as Text. Please see the following code sample which may help you get your desired results.
Sample Code:-

WebCell cell=sheet.Cells["A1"];

cell. PutValue(“00000784647098”);

cell.NumberType = NumberType.Text

As per your other requirement to save the data directly to the data source. I think, you will need to have some event against which you can update your DataSource. So, if you don’t want to use the tick button, you may have to use certain other command button to save the datasource.

Thank You & Best Regards,

Hi,

Thanks for the reply.

Looking at the code sample, I assume I have to set the type cell by cell, as opposed to the whole column?

I load in a blank grid, the user then uses it like excel. When finished they will hit a button I have created to save. What I would like to avoid is that they have to click the 'tick' then the save button. So is it possible I can call the function the 'tick' uses when my save button is pressed?

One other thing, I am looking through the documentation and can't seem to find a way to force only certain values to be added, for example, I would like a cell to only allow a capital I or R. Is this possible?

Thank you for your help so far.

Hi,

Thank you for considering Aspose.

For your query regarding calling the Tick function on your own button, you have to use some JavaScript code to achieve your desired result. Please see the following sample code,
Sample Code:

//Implementing Page_Load event handler

private void Page_Load(object sender, System.EventArgs e)

{

//Checking if there is no postback

if (!IsPostBack)

{

//Adding javascript function to onclick attribute of your Button control

SubmitButton.Attributes["onclick"] = GridWeb1.ClientID + ".updateData(); if (" + GridWeb1.ClientID + ".validateAll())return true; else return false;";

}

}

Now, for using the Validation of a Cell to only Allow R or I, you may use the Cell.CreateValidation method with ValidationType.CustomExpression and specify a regular expression to validate your cell. Please see the following Sample Code,
Sample Code:

// Regular expression.

WebCell cell = Cells["C5"];

cell.CreateValidation(ValidationType.CustomExpression, true);

// R or I reg. expression.

cell.Validation.RegEx = "R|I{1,1}";

Thank You & Best Regards,

thank you!

How do you set the validation for the column instead of each cell? I could use a for loop to loop through all the cells, however just wondering if there is a more efficient method?

Hi,

Well, you have to simply loop through the cells of your column range to apply validation(s). But, if you have implemented GridWeb's data binding mode, you can perform data validation on column level. You may use Worksheet Collection editor at design time and configure the BindColumn collection to set the Validation type for your need. Alternatively, you may use the following sample code at runtime too:

Sample code:

// The "product name" field is required.
Validation v = new Validation();
v.IsRequired = true;
worksheet.BindColumns["ProductName"].Validation = v;

Thank you.