how to bind the data in gridview

Posted by Venkatesh under WPF on 5/17/2011 | Points: 10 | Views : 4778 | Status : [Member] | Replies : 4
Hi,

How to bind the data in gridview in the coding wise. The columns which added dynamically.

Thanks
Venkatesh.P




Responses

Posted by: Lakn2 on: 5/17/2011 [Member] Starter | Points: 25

Up
0
Down
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtToGrid = new DataTable();
dtToGrid.Columns.Add("UserName", typeof(string));
dtToGrid.Columns.Add("Password", typeof(string));
Session["dtToGrid"] = dtToGrid;
}

}

protected void Button1_Click(object sender, EventArgs e)
{
DataTable dtToGrid = (DataTable)Session["dtToGrid"];
DataRow drToGrid = dtToGrid.NewRow();
drToGrid["UserName"] = TextBox1.Text.Trim();
drToGrid["Password"] = TextBox2.Text.Trim();
dtToGrid.Rows.Add(drToGrid);
GridView1.DataSource = dtToGrid;
GridView1.DataBind();
TextBox1.Text = "";
}

protected void Button2_Click(object sender, EventArgs e)
{
//save in Sql Server Table
cn.Open();
DataTable dt = (DataTable)Session["dtToGrid"];
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.DestinationTableName = "userdetails";
copy.WriteToServer(dt);
}
Page.RegisterStartupScript("<script>", "<script>alert('Successfully Saved')</script>");

}
}

Thanks&Regards
LakshmiNarayana Nalluri.

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Venkatesh on: 5/17/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks for replying

In wpf i want bind the data to gridview

Thanks
Venkatesh.P

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 5/18/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Refer this link


http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx

Regards,
Susan

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 9/20/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Are you looking Datagrid in WPF
We can bind datagrid using xaml and code. In xaml we can set itemsource="{Binding DataSource}" ans AutoGenerateColumns="True". Datasource is a public variable that keep the data that you shows in the datagrid. If you set autogeneratecolumns =true, datagrid will generate dynamic columns as per the data.

Refer below links for more info
http://www.wpftutorial.net/DataGrid.html
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response