Sort the grid view after merging cells using OnDataBound asp:net gridview - asp.net

private void GridViewBind()
{
SqlConnection Dbcon = new SqlConnection();
Dbcon.ConnectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
Dbcon.Open();
string expSql = "SELECT [dName],[item],[cnt] FROM [Test1].[dbo].[Test3]
ORDER BY [dName], [cnt] desc";
SqlDataAdapter adAdapter = new SqlDataAdapter(expSql, Dbcon);
DataSet ds = new DataSet();
adAdapter.Fill(ds);
GridView.DataSource = ds.Tables[0];
GridView.DataBind();
ViewState["dt"] = ds.Tables[0];
ViewState["sort"] = "Desc";
}
I drew a table using gridview(asp:gridview) And I made a db for test and bound it
Then, in order to merge cells with the same subject, cells were merged using OnDataBound.
I want to sort by merged subject when cnt is clicked
However, my sort code sorts the entire cnt column, and the merged cells are unraveled.
I need a way to keep the merged cells and do ASC/DESC sorting within them!
protected void OnDataBound(object sender, EventArgs e)
{
for (int i = GridView2.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView2.Rows[i];
GridViewRow previousRow = GridView2.Rows[i - 1];
int j = 0;
if (row.Cells[j].Text == previousRow.Cells[j].Text)
{
if (previousRow.Cells[j].RowSpan == 0)
{
if (row.Cells[j].RowSpan == 0)
{
previousRow.Cells[j].RowSpan += 2;
}
else
{
previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
}
row.Cells[j].Visible = false;
}
}
}
}

I don't have reputation to comment, so i comment here in answer.
Can you add your OnDataBound code where your merged subject?
Also can you make your select query with merged subject as column, then you just need to bind data directly to gridview.
For sorting you can follow below link:
https://www.c-sharpcorner.com/UploadFile/b8e86c/paging-and-sorting-in-Asp-Net-gridview/
https://codepedia.info/gridview-sorting-header-click

Related

can't add all the selected rows to the dataset

I have added records chosen (checkbox) in a datatable shown below, records have been displaying in a datagrid populated using stored procedure. Then on the button click I save all the selected (added) records to the ds in database.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
dr = ds.Tables[0].NewRow();
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
}
}
ds.Tables[0].Rows.Add(dr);
return ds;
}
here my issue is when ds.Tables[0].Rows.Add(dr); i put outside of loop, it adds only one last selected record to ds, and when I put this inside loop, it throws following exception
[pk_scrndRecId] column is read only
and when I have placed this inside if block, it throws following exception:
This row already belongs to this table.
I am not able to understand why this code doesn't add all the selected rows of datagrid to the ds.
please help. thanks!
You have to create a new row and add it to datatable inside the loop and so each time it will create a new instance and add it to your datatable.
Check updated code.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
dr = ds.Tables[0].NewRow();
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
ds.Tables[0].Rows.Add(dr);
}
}
return ds;
}

how to get total of all column if GridView Paging is set as “True”?

this code display total of column in each page of grid view footer I want to display total of all columns in all pages footer
if (e.Row.RowType == DataControlRowType.DataRow)
{
string deb = ((Label)e.Row.FindControl("debit")).Text;
string cred = ((Label)e.Row.FindControl("credit")).Text;
decimal totalvalue = Convert.ToDecimal(deb) - Convert.ToDecimal(cred);
amount += totalvalue;
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = amount.ToString();
Label lblDebAmount = (Label)e.Row.FindControl("debit");
Label lblCredamount = (Label)e.Row.FindControl("credit");
float debtotal = (float)Decimal.Parse(lblDebAmount.Text);
float credtotal = (float)Decimal.Parse(lblCredamount.Text);
totalPriced += debtotal;
totalPricec += credtotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label totallblCAmount = (Label)e.Row.FindControl("totallblDebAmount");
Label totallblCredAmount = (Label)e.Row.FindControl("totallblCredAmount");
totallblCAmount.Text = totalPriced.ToString("###,###.000");
totallblCredAmount.Text = totalPricec.ToString("###,###.000");
}
}
Why do it when the row is data bound? Why not do it when you originally bind the data?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dt = Database.GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
//calculate here by manually adding up the debit/credit column from dt
//or do a separate database call that calculates the sum, ex: select sum(debits), sum(credits) from general_ledger
}
}
It makes sense to let the database do the math calculation for you. You wouldn't need to write your own custom calculation code. Just let the database's sum function handle it.
Here's a little more detail.
//Executes a SqlCommand and returns a DataTable
public class GetDataTable(SqlCommand command)
{
var dt = new DataTable();
using (var connection = new SqlConnection("connectionstring"))
{
command.Connection = connection;
connection.Open();
dt.Load(command.ExecuteReader());
}
return dt;
}
//Executes a SqlCommand and returns a scalar of type T
public static T GetScalar<T>(SqlCommand command)
{
using (var connection = new SqlConnection ("connectionstring"))
{
command.Connection = connection;
connection.Open();
var result = command.ExecuteScalar();
if (result is T)
{
return (T)result;
}
else
{
return (T)Convert.ChangeType(result, typeof(T));
}
}
}
//Load the data into the GridView, then get the credit amount and put the value in a label.
//You'll need to adjust the queries to match your schema
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var tableCommand = new SqlCommand("select columns from table");
GridView1.DataSource = GetDataTable(tableCommand);
GridView1.DataBind();
var creditCommand = new SqlCommand("select sum(stat_amount) from tablename where stat_flag='c'");
var credits = GetScalar<decimal>(creditCommand);
Label1.Text = credits;
}
}

jqgrid footervalue not correct if rows is empty

I have a jqgrid (ver 4.6.0.0), I show a grant total on footer of a amount column. If user insert a new row or delete a row, it should recalculate the total amount, and show it on footer. It works fine until the grid is empty(no any rows), I mean if user delete the last row on grid, the code still calculate the correct amount ( 0 ), but the footer still show the total amount it was, not 0.
Here is my code:
protected void Jqgrid1_DataRequested(object sender, JQGridDataRequestedEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1 = _ds.Tables[1] as DataTable;
CalcVoucherAmt(dt1);
}
private void CalcVoucherAmt(DataTable _dt)
{
decimal amtSum = 0;
foreach (DataRow dr in _dt.Rows)
{
amtSum += obj2decimal(dr["DETAIL_AMT"]);
}
Jqgrid1.Columns.FromDataField("DETAIL_AMT").FooterValue = amtSum.ToString();
}
protected void Jqgrid1_RowAdding(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1 = _ds.Tables[1] as DataTable;
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["DATA_SEQ"] };
DataRow row = dt1.NewRow();
row["UNIQ_KEY"] = System.Guid.NewGuid().ToString();
row["DETAIL_AMT"] = amt; //user input
dt1.Rows.InsertAt(row, dt1.Rows.Count);
Session["myApplicationForm"] = _ds;
Jqgrid1.DataSource = dt1;
Jqgrid1.DataBind();
}
protected void Jqgrid1_RowEditing(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1=_ds.Tables[1] as DataTable;
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["DATA_SEQ"] };
DataRow rowEdited = dt1.Rows.Find(e.RowKey);
rowEdited["DETAIL_AMT"] = amt; //user input
Session["myApplicationForm"] = _ds;
Jqgrid1.DataSource = dt1;
Jqgrid1.DataBind();
}
protected void Jqgrid1_RowDeleting(object sender, JQGridRowDeleteEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1 = _ds.Tables[1] as DataTable;
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["DATA_SEQ"] };
DataRow rowToDelete = dt1.Rows.Find(e.RowKey);
if (rowToDelete != null)
dt1.Rows.Remove(rowToDelete);
}
and in aspx, the code is
<cc1:JQGrid ID="Jqgrid1" runat="server" Height="100%" Width="100%" AutoWidth="true" PagerSettings-PageSizeOptions="[]"
OnRowAdding="Jqgrid1_RowAdding"
OnRowEditing="Jqgrid1_RowEditing"
OnDataRequested="Jqgrid1_DataRequested"
OnRowDeleting="Jqgrid1_RowDeleting">
</cc1:JQGrid>
any idea?
Why you decide that jqGrid "should recalculate the total amount" automatically? If you insert a row using or delete a row you can specify callback which will be called at the end of Add/Delete operation. Inside of the callback you can use getCol to recalculate the total amount and to use footerData to set new value in the footer.
Alternatively you can trigger reloadGrid which will sort new data correctly, recalculate the total amount and to place it on the footer row. Both ways will work.
The code which you posted seems to have mo relation to jqGrid. It don't show which editing mode and how you use to Add/Delete rows. So I can't give you more detailed implementation tips.

Add To The Top in the DataList

This My code For binding My DataList ,and every item in datalist have a different button ,
the items sorted correctly by date but the index for each item not sorted with it ,
ex:
when insert a new data in employees table the data shows correct(sorted by date),the last employee was insert into employee table shows in the first item and take the index 0.
I want to know how i can to make his index The Last Index in my old data + 1 ?
private void bind()
{
da2 = new SqlDataAdapter("select * from employees order by insert_date desc", m_SqlConnection);
DataSet dataSet2 = new DataSet();
da2.Fill(dataSet2, "det");
DataList1.DataSource = dataSet2.Tables["det"];
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button btn = (Button)e.Item.FindControl("button4");
Button btn2 = (Button)e.Item.FindControl("button1");
da2 = new SqlDataAdapter("select insert_stat from insert_detail where user_id='" + int.Parse(Session["id"].ToString()) + "'", m_SqlConnection);
DataSet dataSet2 = new DataSet();
da2.Fill(dataSet2, "chk");
if (dataSet2.Tables["chk"].Rows[e.Item.ItemIndex]["insert_stat"].ToString() == "accept")
{
btn.Visible = true;
btn2.Visible = true;
}
else
{
if (dataSet2.Tables["chk"].Rows[e.Item.ItemIndex]["insert_stat"].ToString() == "reject")
{
btn.Visible = false;
btn2.Visible = false;
}
}
}
This is more a SQL problem than a DataList issue.
I recommend that you rewrite your SQL in the bind() method to do a JOIN on the insert_detail table to get the accept or reject value out of the table and into the bound data set for the data list. This serves two benefits:
It eliminates the database call in the DataList1_ItemDataBound() method, which if you have dozens or hundreds of rows, then that is dozens or hundreds less database calls.
It allows you to put the insert_stat value into a HiddenField control in your DataList and then check the value in the DataList1_ItemDataBound event, like this:
HiddenField theHiddenField = e.Item.FindControl("HiddenField1") as HiddenField;
// Make sure we found the control, because the as operator
// returns null for a failed cast
if(theHiddenField != null)
{
if(theHiddenField.Value.ToLower() == "accept")
{
btn.Visible = true;
btn2.Visible = true;
}
else
{
btn.Visible = false;
btn2.Visible = false;
}
}

how to get gridview all rows data when using paging

I have a gridview with paging (page size 10), which contains Course details. when i am saving gridview data into database it save only 10 record (according to page).
is there any to get all gridview all rows.
e.g. if my gridview contains 25 rows with paging and when i save data into database then all 25 rows data insert into database?
please help...
Here is my code
DataTable dt = new DataTable();
dt.Columns.Add("row_index");
dt.Columns.Add("edited_value");
IList<int?> SeqNos = new List<int?>();
foreach (GridViewRow gvr in gvViolationCodes.Rows)
{
TextBox tb = (TextBox)gvr.FindControl("txtSeqNo");
Label lblSysid = (Label)gvr.FindControl("lblSysID");
HiddenField hf = (HiddenField)gvr.FindControl("HiddenField1");
if (tb.Text != hf.Value)
{
DataRow dr = dt.NewRow();
SeqNos.Add(Convert.ToInt32(tb.Text));
dr["row_index"] = lblSysid.Text;
dr["edited_value"] = tb.Text;
dt.Rows.Add(dr);
}
}
You need to save the data in temporary storage in PageIndexChanging event of GrIdView. On final save send the datatable to Sql Server Procedure or loop through the datatable and save it in database. Follow the steps:
protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
fnStoreGridData();
}
private void fnStoreGridData()
{
DataTable TempDataTable = new DataTable();
if(Session["Data"]!=null){
TempDataTable = Session["Data"] as DataTable;
}
else{
TempDataTable.Columns.Add("exCourse", typeof(string)) ;
TempDataTable.Columns.Add("exUniversityCourse", typeof(string)) ;
}
foreach (GridViewRow row in gvExportCourse.Rows)
{
Label exCourse = (Label)row.FindControl("gvlblCourseName");
Label exUniversityCourse = (Label)row.FindControl("gvlblUniversityCourseName");
if (exCourse != null)
{
if (!string.IsNullOrEmpty(exCourse.Text))
{
TempDataTable.Rows.Add(exCourse.Text, exUniversityCourse.Text);
//TempDataTable is your datatable object. Make sure that datatable contains these columns
}
}
}
}
In Final Save:
protected void button_Click(object s, EventArg e)
{
fnStoreGridData(); //Here this will bind the data of current page
DataTable TempDataTable = new DataTable();
if(Session["Data"]!=null){
TempDataTable = Session["Data"] as DataTable;
}
//Now loop through datatable and store the values
foreach(DataRow in TempDataTable.Rows)
{
CourseInformation course = new CourseInformation();
course.AddCourseMaster(dr["exCourse"].ToString(), dr["exUniversityCourse"].ToString());
}
label1.Text = "Course saved successfully.";
}

Resources