TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
virupakshi jekin
1.5k
269
4k
Dynamically Creating Data-table and Binding To Grid-view
Aug 30 2018 4:39 AM
Dynamically Creating DataTable and Binding To GridView i want store all gridview data in database at time.
Source aspx page
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
asp:TextBox
ID
=
"txt_id"
runat
=
"server"
placeholder
=
"ID"
>
</
asp:TextBox
>
<
br
/>
<
asp:TextBox
ID
=
"txt_name"
runat
=
"server"
placeholder
=
"Name"
>
</
asp:TextBox
>
<
br
/>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"Button"
onclick
=
"btn_1_Click"
/>
<
asp:GridView
ID
=
"GridView1"
runat
=
"server"
>
</
asp:GridView
>
<
asp:Button
ID
=
"Button2"
runat
=
"server"
Text
=
"Button"
OnClick
=
"btn_2_Click"
/>
</
div
>
</
form
>
CS file
protected
void
btn_1_Click(
object
sender, EventArgs e)
{
GridView1.Visible =
true
;
createnewrow();
}
public
void
createnewrow()
{
DataTable mytable =
new
DataTable();
if
(ViewState[
"Row"
] !=
null
)
{
mytable = (DataTable)ViewState[
"Row"
];
DataRow dr =
null
;
if
(mytable.Rows.Count > 0)
{
dr = mytable.NewRow();
dr[
"Id"
] = txt_id.Text;
dr[
"Name"
] = txt_name.Text;
mytable.Rows.Add(dr);
ViewState[
"Row"
] = mytable;
GridView1.DataSource = ViewState[
"Row"
];
GridView1.DataBind();
}
}
else
{
mytable.Columns.Add(
"Id"
,
typeof
(
int
));
mytable.Columns.Add(
new
DataColumn(
"Name"
,
typeof
(
string
)));
DataRow dr1 = mytable.NewRow();
dr1 = mytable.NewRow();
dr1[
"Id"
] = txt_id.Text;
dr1[
"Name"
] = txt_name.Text;
mytable.Rows.Add(dr1);
ViewState[
"Row"
] = mytable;
GridView1.DataSource = ViewState[
"Row"
];
GridView1.DataBind();
}
}
// this is my save button
protected
void
btn_2_Click(
object
sender, EventArgs e)
{
con.Open();
SqlCommand com =
new
SqlCommand(
"tbl_adda"
, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue(
"@Id"
, Convert.ToInt32(txt_id.Text));
com.Parameters.AddWithValue(
"@Name"
, txt_name.Text.ToString());
com.ExecuteNonQuery();
con.Close();
}
please help me
Reply
Answers (
3
)
C# to Vb.Net Code conversion
import excel with for loop in table