C# .NET - How to Enter data in gridview cells, without using textbox

Asked By suresh kotte on 29-Jan-15 03:02 PM

    How to Enter data in gridview cells, without using textbox. Only enter data in gridview cells and save it then it goes to sql server. So please how to enter the data in gridview cells only. The image file also here.


dipa ahuja replied to suresh kotte on 09-Jan-12 01:05 PM
To set the cell values you will required the row index and cell index

dataGridView1.Rows[0].Cells[0].Value = 10;
dataGridView1.Rows[0].Cells[1].Value = "Dipa";
dataGridView1.Rows[0].Cells[2].Value = "India";

[)ia6l0 iii replied to suresh kotte on 09-Jan-12 01:16 PM
GridView + SQLDataSource is what you need:

Create a SQLDataSource and specify the connection string and the InsertCommand.
<asp:sqldatasource ID="customSqlDataSource" runat="server" 
        InsertCommand="Insert into mytable(col1, col2, col3) values (@val1, @val2, @val3)"

In your Gridview,
a) set the ShowFooter property to true and
b) Create an OnRowCommand event handler.
c) Also specify the Empty data template as shown below.
d) Set the DataSourceID to the SQLDataSource created in above step.

<asp:GridView ID="myGridView" runat="server" DataSourceID="customSqlDataSource" ShowFooter="true"
            type first val:<asp:TextBox runat="server" ID="firstcol" />
            type seond val:<asp:TextBox runat="server" ID="secondcol" />
            type third val:<asp:TextBox runat="server" ID="thirdcol" />
            <asp:Button runat="server" ID="insertdata" CommandName="insert" Text="Insert data"/>

Handle the command that you set in the RowCommand event of the Gridview. 

private List<SqlParameter> params = new List<SqlParameter>();

protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "insert")
            TextBox firstVal = myGridView.Controls[0].Controls[0].FindControl("firstcol") as TextBox;
            SqlParameter fv = new SqlParameter("@val1", SqlDbType.VarChar, 30);
            fv .Direction = ParameterDirection.Input;
            fv.Value = FirstName.Text;
    ...  and so on

Add those params from the params collection collected in the rowcommand event to the sqldatasource command. Sample below:

protected void customsqldatasource_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        foreach (SqlParameter prm in params)

That should be it.

Jitendra Faye replied to suresh kotte on 09-Jan-12 01:22 PM
As you told that you want to insert data in gridview cell%2c so insert data in cell you have to take any input control in
%3cbr/%3egridview cell. without any input control you can not insert data in cell
%3cbr/%3eput textbox or any other input control in gridview itemtemplate.