Tuesday, 20 May 2014

C# code for binding the database table in grid view (OR) C# code for edit the table data value under dataset (OR) how to edit the loaded dataset value dynamically



Indroduction
Now I will explain binding the database table in grid view (AND)
How to edit the table data value under dataset (AND) how to edit the loaded dataset value dynamically


Use the below code   and stored procedure

Stored Procedure
create procedure [dbo].[sp_bindproduct]
as begin

select *
from dbo.Product_Master

end

c# code
     
public void showgrid()
        {
//make connection to DB
            using (SqlConnection CON1 = new SqlConnection(@"Data Source=PROV-SER-001\SQLEXPRESS;Initial Catalog=Jeds;Integrated Security=True"))
            {


//call the stored procedure
                SqlCommand cmd_bindproduct = new SqlCommand("sp_bindproduct", CON1);

//execute the stored procedure

                cmd_bindproduct.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DA = new SqlDataAdapter(cmd_bindproduct);

//load the Db value into Dataset

                DataSet DS = new DataSet();
                DA.Fill(DS);
                //check the dataset if the value is 0 then become its N/A
//check dynamically              
 for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
                {
//load the coloumn value to string variable
                    string Svdiscountcost = DS.Tables[0].Rows[i]["PM_Svdiscountcost"].ToString();

                    if (Svdiscountcost == "0" || Svdiscountcost == null)
                    {
//Assign or edit the dataset value
                        DS.Tables[0].Rows[i]["PM_Svdiscountcost"] = "N/A";
                    }

                    string DiscountGM = DS.Tables[0].Rows[i]["PM_DiscountGM"].ToString();
                    if (DiscountGM == "0" || DiscountGM == null)
                    {
//Assign or edit the dataset value

                        DS.Tables[0].Rows[i]["PM_DiscountGM"] = "N/A";
                    }

                    string discountcostpallet = DS.Tables[0].Rows[i]["PM_Totaldiscountcost"].ToString();
                    if (discountcostpallet == "0" || discountcostpallet == null)
                    {
//Assign or edit the dataset value

                        DS.Tables[0].Rows[i]["PM_Totaldiscountcost"] = "N/A";
                    }


                }
//after edit the dataset Binding to the grid view
                GridView1.DataSource = DS;
                GridView1.DataBind();
            }
        }


I hope this will helpfull for you! Thanks

No comments:

Post a Comment