Adding new row to datatable's top - asp.net

When we use datatable.newrow command, a new empty row added to bottom of rows. However I want newrow to added to top of datatable. How can I make it?

You use the NewRow to create a row with the same columns. To actually get it into the DataTable, you've got to do
myDataTable.Rows.InsertAt(myDataRow, 0);
Where 0 is the index you want to insert it at.

Here is the best example to add row in table
DataRow newRow = myDataTable.NewRow();
newRow[0] = "0";
newRow[1] = "Select one";
myDataTable.Rows.InsertAt(newRow, 0);
It's set the row on first

This One is Wrong
myDataTable.Rows.InsertAt(0,myDataRow);
Please use the below line instead of that
myDataTable.Rows.InsertAt(myDataRow,0);

Related

PHPExcel insert new blank row after the last row in the sheet

How can I insert new blank row? ( for separating my rows ) .
My code is:
$num_rows = $xls->getActiveSheet()->getHighestRow();
//there need to insert a blank new row
$sheet->fromArray($header,null, "A".($num_rows + 1));
How can I solve this problem? Please, help me.
A blank row after the last row in the sheet? Why? Blank rows at the end of worksheet just take up memory.
But if you want to write data to cells beyond the end of the spreadsheet, then just do so....
$num_rows = $xls->getActiveSheet()->getHighestRow();
// Add 2 instead of 1
$sheet->fromArray($header,null, "A".($num_rows + 2));

How to insert a constant value into a column using ASP.Net SqlBulkCopy

In the below code, I am trying insert the records from excel to Database table, but an additional column is not passed through the excel, which has to be populated with a constant value(foreach loop with a different value) assigned from the requested page.
string CONSTANTVALUE="Test";
bulkCopy.DestinationTableName = "TABLE NAME";
bulkCopy.ColumnMappings.Add("TABLECOLUMN1", "EXCELCOLUMN1");
bulkCopy.ColumnMappings.Add("TABLECOLUMN2", "EXCELCOLUMN2");
bulkCopy.ColumnMappings.Add("TABLECOLUMN3", CONSTANTVALUE);
bulkCopy.WriteToServer(dr);
But the code doesn't work. Any ideas?
You can do it, by changing your command text. As below
string CONSTANTVALUE="Test";
OleDbCommand command=new OleDbCommand("select *,"+CONSTANTVALUE+" as [ConstantCol] from [sheet$]",ObleDbCon);
using (DbDataReader dr = command.ExecuteReader())
{
bulkCopy.DestinationTableName = "TABLE NAME";
bulkCopy.ColumnMappings.Add("TABLECOLUMN1", "EXCELCOLUMN1");
bulkCopy.ColumnMappings.Add("TABLECOLUMN2", "EXCELCOLUMN2");
bulkCopy.ColumnMappings.Add("TABLECOLUMN3", "ConstantCol");
bulkCopy.WriteToServer(dr);
}
I assume your dr is a reader of some kind. How is it populated? It may be possible to select a default value into a column and map that. Something like this (sql syntax)
select
EXCELCOLUMN1,
EXCELCOLUMN2,
'ConstantValueFromPage' as EXCELCUSTOM
from
sheet1
Then have:
bulkCopy.ColumnMappings.Add("TABLECOLUMN3", "EXCELCUSTOM");
HTH
Did you try setting a default value for you column in database? I think it's the most easiest way, as after inserting any record, the default value would also get inserted into the specified column (it acts like triggers).

Edit the code of a GridView column asp.net

I've been looking for a way to control the text that appears in a particular column of a GridView.
For example consider a database with two tables Student and Class.
I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)
I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?
1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.
2- More than one way to do what you want:
For example:
you can add a column to your data table (empty column ), and fill it later through using Sum() aggregate function in a query.
DataTable result_dt = DAL_Helper.Return_DataTable(sqlSelect);//your original query
result_dt.Columns.Add("NumberOfStudent");
result_dt.Columns["NumberOfStudent"].DataType = typeof(string);
result_dt.Columns["NumberOfStudent"].MaxLength = 255;
if (result_dt.Rows.Count > 0)
{
for (int i = 0; i < result_dt.Rows.Count; i++)
{
//Here u can fill your new empty column.
}
}
result_dt.AcceptChanges();
After you return your customized data table(as a data source), you can bind it to the grid view.
Another solution : add an empty column to the grid view and in the RowDataBound event of the grid view you can fill this column through some loop and you can use LINQ to help you in getting the summation.

Column Value of multiselect rows in jqgrid

Its like I have multiselect option in Jqgrid in which i want to pass the selected rows value to the server and based on the value i will delete the rows. I dont want to have the ids to do my work. For the Single row i get the cell value and delete using the same. But for multi select its not the case. In getGridParam('selarrrow'); I use this to fetch the selected rows bu the values are not getting populated. Please help me out in doing the same
When i use the following code as i saw in some sample question i can fetch the value for the single row selection but when i select multiple rows then i pass it says like "FALSE" or "UNDEFINED". What can be the issue. var grid = jQuery('#list'); var sel_id = grid.jqGrid('getGridParam', 'selarrrow'); var myCellData = grid.jqGrid('getCell', sel_id, 'CountryId');
I got this done by using the for loop for getting the selected rows id
i did some thing like this and I am able to fetch the value. Thought this might help others too please check it
var myrow;
var id = jQuery("#List").jqGrid('getGridParam','selarrrow');
if(id.length)
{
for (var i=0;i<id.length;i++) // For Multiple Delete of row
{
myrow = jQuery("#List").jqGrid('getCell',id[i],'ticker');
}
}

How to set dropdown's firsl Item -Select- and remaining Items from a column of table?

I m using a dropdown to display "Location" field of a table. I want to set first item of dropdowm as "-Select Location-". I can't set tables first record as "Select" because table is stroed in xml format. And table file is generated dynamicaly.
I am currentaly using as
ddlLocationName.Dispose();
ddlLocationName.AppendDataBoundItems = true;
ddlLocationName.Items.Add("Select Location");
ddlLocationName.DataSource = _section.GetLocations();
ddlLocationName.DataBind();
ddlLocationName.AppendDataBoundItems = false;
but data is binded repeatedly.
What will be the solution for this problem?
Thaks in advance.
After you have databound, then call ddlLocationName.Items.Insert(0, "Select Location");
Example:
ddlLocationName.Items.Clear();
ddlLocationName.DataSource = _section.GetLocations();
ddlLocationName.DataBind();
ddlLocationName.Items.Insert(0, "Select Location"); // Adds the item in the first position
Access the items in the form of ListItems:
ListItem li = new ListItem("Select Location","-1");
ddlLocationName.Items.Add(li);
After you have bound your other data, use:
ddlLocationName.SelectedValue = "-1";
Also, you can add the values of your table in similar way to the ListItem first.

Resources