ADD manually Columns in GridView control in asp.net

we will learn here insert data to sql server and bind it to Gridview control in asp.net

first create database in sql server then create table in sql server.

The Databse name = Example and Table name = UserMst

we have here four columns ID, Name, Surname, City in Table.

Design web form in asp.net as follow:

Add manually create columns in gridview control
Add manually create columns in gridview control

 

Now, set AutoGenerateColumns = false for hide autogenerated columns in gridview.

Add manually create columns in gridview control
Add manually create columns in gridview control

After set AutogenerateColumns = false then add manually columns in gridview as follow:

Add manually create columns in gridview control
Add manually create columns in gridview control

 

we can add manually columns in gridview as follow:

Add manually create columns in gridview control
Add manually create columns in gridview control

we can also add manually columns by writting below code in HTML code of web page:

<asp:GridView ID=”GridView1″ runat=”server” Width=”247px”>
<Columns>
<asp:BoundField DataField=”Name” HeaderText=”Name” SortExpression=”Name” />
<asp:BoundField DataField=”Surname” HeaderText=”Surname”
SortExpression=”Surname” />
<asp:BoundField DataField=”city” HeaderText=”City” SortExpression=”city” />
</Columns>
<HeaderStyle BackColor=”#FF99FF” />
</asp:GridView>

Add manually create columns in gridview control
Add manually create columns in gridview control

 

write below code in Button control for add data to sql server and then select data and bind it to gridview control.

protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“Data Source=’.\\SQLEXPRESS’;Integrated Security=’true’;Initial Catalog=’Example'”);
SqlDataAdapter Adp = new SqlDataAdapter(“INSERT INTO UserMst values (‘” + txtname.Text +”‘,'”+  txtsurname.Text +”‘,'”+  txtcity.Text +”‘)”, con);
DataTable DT = new DataTable();
Adp.Fill(DT);

SqlDataAdapter Adpselect = new SqlDataAdapter(“select * from UserMst”, con);
Adpselect.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}

Add manually create columns in gridview control
Add manually create columns in gridview control

I hope this gridview control add columns example will help you….

Leave a Reply

Your email address will not be published. Required fields are marked *