Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I start to create one coding method,for using Gridview. I succefully binding through this. but i couldn't save though this architecture...how to save all rows in gridview. plz help me....

C#
 public partial class WebForm2 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.GridView gvDynamicArticle;
        protected void Page_Load(object sender, EventArgs e)
        {
          
            TextBox1 = new TextBox();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            gvDynamicArticle = new GridView();
            TextBox1 = new TextBox();
            TextBox1.ID = "TextBox1";
            TextBox1.Style["Position"] = "Absolute";
            TextBox1.Style["Top"] = "25px";
            TextBox1.Style["Left"] = "100px";

            SqlConnection con = new SqlConnection("Data Source=ASPSERVER\\SQLEXPRESS;Initial Catalog=Varkeys;Integrated Security=True");
            con.Open();
            SqlCommand com = new SqlCommand("Select * from CM_MT_REF_CODE", con);
           
            SqlDataAdapter ada = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            for (int i = 0; i < ds.Tables.Count; i++)
            {

                if (ds.Tables[i].Rows.Count > 0)
                {
                    // GridView gvDynamicArticle = new GridView();
                    gvDynamicArticle.Width = Unit.Pixel(700);
                    gvDynamicArticle.BorderWidth = Unit.Pixel(5);
                    gvDynamicArticle.Caption = "<div id=\"nifty\" class=\"PostCategory\"> <b class=\"rtop\"><b class=\"r1\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b>" + ds.Tables[i].Rows[0]["CNAME"].ToString() + " Articles<b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b class=\"r1\"></b></b></div>";
                    gvDynamicArticle.AutoGenerateColumns = false;
                    gvDynamicArticle.ShowFooter = true;

                    for (int xGH = 0; xGH < ds.Tables[i].Columns.Count; xGH++)
                    {
                        string variable = ds.Tables[i].Columns[xGH].ColumnName.ToString();
                        if (variable == ds.Tables[i].Columns[xGH].ColumnName.ToString())
                        {
                            TemplateField tf = null;
                            tf = new TemplateField();
                            tf.HeaderTemplate = new DynamicGridViewTextTemplate(variable, DataControlRowType.Header);
                            tf.ItemTemplate = new DynamicGridViewTextTemplate(variable, DataControlRowType.DataRow);
                            tf.ControlStyle.BackColor = Color.LightBlue;

                            gvDynamicArticle.Columns.Add(tf);
                        }

                    }



                    gvDynamicArticle.DataSource = ds.Tables[i];
                    gvDynamicArticle.DataBind();

                 

                    phDynamicGridHolder.Controls.Add(gvDynamicArticle);
                }
            }

          

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
        }
       
        
   
    }
}
public class DynamicGridViewTextTemplate : ITemplate
{
    string _ColName;
    DataControlRowType _rowType;
   

    public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
    {
        _ColName = ColName;
        _rowType = RowType;
    }
  
    public void InstantiateIn(System.Web.UI.Control container)
    {
        switch (_rowType)
        {
            case DataControlRowType.Header:
                Literal lc = new Literal();
                lc.Text = "<b>" + _ColName + "</b>";
                container.Controls.Add(lc);
                break;
            case DataControlRowType.DataRow:
                TextBox lbl = new TextBox();
                lbl.DataBinding += new EventHandler(this.lbl_DataBind);
                container.Controls.Add(lbl);
                break;
          
            default:
                break;
        }
    }


    private void lbl_DataBind(Object sender, EventArgs e)
    {
        TextBox lbl = (TextBox)sender;
        GridViewRow row = (GridViewRow)lbl.NamingContainer;
        lbl.Text = DataBinder.Eval(row.DataItem, _ColName).ToString();
    }

}
Posted
Updated 9-Apr-10 5:05am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900