5
Answers

how to extract a selectedrow data to a datatable?

Mark Uy

Mark Uy

15y
4.1k
1
the title of the thread says it all.

how?
Answers (5)
0
shyamala

shyamala

NA 71 0 15y

if u r using Datalist means
dt=con.DBExecute("select * from tablename where id='"+id+"'");
Datalist.DataSource=dt;
Datalist.DataBind();
0
shyamala

shyamala

NA 71 0 15y

protected
void LinkButton_Click(object sender, EventArgs e)
{
        LinkButton lb = (LinkButton)sender;
        GridViewRow Gvrow = (GridViewRow)lb.Parent.Parent;
        string id1= GridView1.DataKeys[Gvrow.RowIndex].Values["id"].ToString();
        Response.Redirect("nextpage.aspx?vprofile=" + id1);
}
0
Nilanka Dharmadasa

Nilanka Dharmadasa

NA 3k 0 15y

Friend,
 
First you should make sure the columns in datatable and datagridview are same.
Then you can use following code.
 
if (dataGridView1.SelectedRows == null)
return;
DataGridViewRow row = dataGridView1.SelectedRows[0];
if (row != null)
{
DataRow r = dtable.Rows.Add(row.Cells[0].Value, row.Cells[1].Value);
dtable.AcceptChanges();
}
 
 
If this helps you, please do not forget to mark Do you like this answer checkbox.
0
Mark Uy

Mark Uy

NA 130 0 15y
i believe those codes came from this site: http://www.dotnettutorials.com/tutorials/database/gridview-to-datatable-cs.aspx

what does the line "namespace DotNetTutorials.Tutorial" mean?
0
Lalit M

Lalit M

NA 4.2k 59.1k 15y
Hi,

You can use this code sample for Select GridView Row to DataTable

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Web.Configuration;
using System.Data.SqlClient;

/// <summary>
/// Summary description for GridViewToDataTable
/// </summary>
namespace DotNetTutorials.Tutorial
{
public class GridViewToDataTable
{
/// <summary>
/// Default constructor for GridViewToDataTable
/// </summary>
public GridViewToDataTable()
{
}

/// <summary>
/// Gets all columns from Table1
/// </summary>
/// <returns>DataTable of all columns in Table1</returns>
public static DataTable GetAllData()
{
DataTable allData = new DataTable();
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
try
{
SqlCommand cmd = new SqlCommand("GetAllData", connection);
cmd.CommandType = CommandType.StoredProcedure;

connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(allData);
connection.Close();
}
catch
{
connection.Close();
}
return allData;
}
}
}

You can also show this article from this link
For more info Q/A.