I want to fill GridView ,But i dont want to use datasource..I want to use the method,thru which i add the data using row by row

As in VB.net

Dim row0 As String() = {"A","B","C"};
            Me.DataGridFile.Rows.Add(row0)

.

public partial class refresh : System.Web.UI.Page
{
    string query;
    SqlCommand cmd;
    SqlConnection conn;
    SqlDataAdapter da;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
                conn = new SqlConnection("Data Source=MANSI\\SQLEXPRESS;Initial catalog=user;Integrated Security=true;");
        conn.Open();
        query="select * from info";
        cmd=new SqlCommand (query ,conn ) ;
        da=new SqlDataAdapter (cmd) ;
        ds = new DataSet();
        da.Fill (ds);
        GridView1 .DataSource =ds;
        GridView1 .DataBind ();
        conn.Close ();
    }

Recommended Answers

All 7 Replies

You can use a datatable instead

DataSet dsResult = new DataSet();
DataRow drRev;
DataTable dtRev = new DataTable();
dtRev.Columns.Add("Name");
dtRev.Columns.Add("Number");
For(int i =0; i < 3; i++)
{
     drRev = dtRev.NewRow();
     drRev[0] = "Ramon"; 
     drRev[1] = i.ToString();
     dtRev.Rows.Add(drRev);
}
dsResult.Tables.Add(dtRev);
GridView1.DataSource = dsResult;
GridView1.DataBind();

and that case you can manipulate the data before bind the datagrid hope that help you.

This is C# forum. Ask web related question in asp.net forum.
You can bind one dim array with grdiview,

String []ar={"A","B","C"};
 GridView1.DataSource=ar; 
 GridView1.DataBind();

Use may bind dataTable:

DataTable dt=new DataTable();
dt.Columns.Add("No",typeof(int));
dt.Columns.Add("Name"); // default type is string
dt.Rows.Add(1,"A");
dt.Rows.Add(2,"B");
GridView1.DataSource=dt;
GridView1.DataBind();

thx very much one problem is solved,I want to ask that is there any to set tag in Gridview,As In listview we have tag & for each row it is different, I want to delete the row from the gridview, on what basis can i do dat..Just give me hint..Not the entire code..I will be very thankfull to U..

Get the Gridview's selected row handle or associated data row and call .Delete() on the DataRow then .AcceptChanges() on the associated data table.

Hi Mansi, like Tags in ListView, you may try FormatString for each item using foreach loop and then bind back again to your GridView.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.