According to your description, I would suggest you to create two tables. One contains slno Name and Dept. The other contains slno and Age. Please follow the demo below and run the code below in the SQL.
USE [teset2]
GO
/****** Object: Table [dbo].[tb_company] Script Date: 05/10/2012 16:25:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tb_company](
[slno] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](10) NOT NULL,
CONSTRAINT [PK_tb_company] PRIMARY KEY CLUSTERED
(
[slno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[tb_company] ON
INSERT [dbo].[tb_company] ([slno], [name]) VALUES (1, N'a')
INSERT [dbo].[tb_company] ([slno], [name]) VALUES (2, N'b')
INSERT [dbo].[tb_company] ([slno], [name]) VALUES (3, N'c')
INSERT [dbo].[tb_company] ([slno], [name]) VALUES (4, N'd')
SET IDENTITY_INSERT [dbo].[tb_company] OFF
/****** Object: Table [dbo].[tb_Age] Script Date: 05/10/2012 16:25:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_Age](
[slno] [int] IDENTITY(1,1) NOT NULL,
[Age] [int] NOT NULL,
CONSTRAINT [PK_tb_Age] PRIMARY KEY CLUSTERED
(
[slno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tb_Age] ON
INSERT [dbo].[tb_Age] ([slno], [Age]) VALUES (1, 20)
INSERT [dbo].[tb_Age] ([slno], [Age]) VALUES (2, 25)
INSERT [dbo].[tb_Age] ([slno], [Age]) VALUES (3, 45)
INSERT [dbo].[tb_Age] ([slno], [Age]) VALUES (4, 70)
SET IDENTITY_INSERT [dbo].[tb_Age] OFF
/****** Object: ForeignKey [FK_tb_Age_tb_company] Script Date: 05/10/2012 16:25:47 ******/
ALTER TABLE [dbo].[tb_Age] WITH CHECK ADD CONSTRAINT [FK_tb_Age_tb_company] FOREIGN KEY([slno])
REFERENCES [dbo].[tb_company] ([slno])
GO
ALTER TABLE [dbo].[tb_Age] CHECK CONSTRAINT [FK_tb_Age_tb_company]
GO
Then obtain the data from two datatables and show in the gridview as follows:
kathir_cs
Member
20 Points
88 Posts
Bind Default values in gridview
May 05, 2012 02:48 PM|LINK
Hi,
I am using gridview to show company details,
Some of the fields are contstant,
slno Name Dept Age
1 a IT
2 b IT
3 c IT
4 d IT
Slno,name,dept are constant values
Age values are taken from database.
slno starts from 1 to 4 only. how to display in gridview
Thank u in advance
sriramabi
Contributor
4351 Points
1277 Posts
Re: Bind Default values in gridview
May 05, 2012 03:30 PM|LINK
u get age values in databae and bind gridvie.than s/no,name and dept value is manualy set one lable from inside gridview...
thank u
kathir_cs
Member
20 Points
88 Posts
Re: Bind Default values in gridview
May 05, 2012 04:32 PM|LINK
HI,
Reference coding
ZeeshanAnsar...
Participant
878 Points
264 Posts
Re: Bind Default values in gridview
May 05, 2012 04:56 PM|LINK
kathir_cs,
create a table for constant values
insert the constants values into that table
age may be stored into some other table right?
then in the sp prepare a query which fetches data from both the table as per your needs [join]
then bind the same to gridview.
I hope it hepls you.
Please 'Mark as Answer' if this post helps you.
Catherine Sh...
All-Star
23382 Points
2490 Posts
Microsoft
Re: Bind Default values in gridview
May 10, 2012 08:36 AM|LINK
Hi kathir_cs,
According to your description, I would suggest you to create two tables. One contains slno Name and Dept. The other contains slno and Age. Please follow the demo below and run the code below in the SQL.
Then obtain the data from two datatables and show in the gridview as follows:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="slno" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="slno" HeaderText="slno" InsertVisible="False" ReadOnly="True" SortExpression="slno" /> <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:teset2ConnectionString %>" SelectCommand="SELECT tb_company.slno, tb_company.name, tb_Age.Age FROM tb_Age INNER JOIN tb_company ON tb_Age.slno = tb_company.slno"> </asp:SqlDataSource> </div> </form> </body>Note: Please modify the ConnectionStrings:teset2ConnectionString to meet your reqirement.
Best wishes,
Feedback to us
Develop and promote your apps in Windows Store