C# .NET - How to insert the values in the gridview without dataset,datatable.

Asked By karthik karthik on 14-Sep-10 03:07 AM
Hai Freinds

I am having two textbox name empame,empid  i n that i am going to enter the values and insert into the grid.its is simple.but here i should not use these Datasoruce,dataset,datatable.datarow.directly if i enter it sholud go and insert in the gridview.

Tx in advance
Anand Malli replied to karthik karthik on 14-Sep-10 03:13 AM
What you want to do?? you just want to use DataTAble,DataRow or also you do not want to use Array,HashTable Collection as well..coz if you put those values inside those collection,you grid will get bind easily

do you want that??

let me know
Thanks
Sagar P replied to karthik karthik on 14-Sep-10 03:16 AM
You can insert it like this;

Say you have tow columns empid and empname in gridview, and 2 textboxes txtEmpId and txtEmpName, now you want to insert new row with data in these textboxes. So you can use this code on button click;

//Create the Row

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

//Add the two Columns

row.Cells.AddRange(CreateCells());

//get a reference to the table that holds this row

Table tbl = (e.Row.Parent as Table);

//Add the row at the end of the list, but before the footer.

tbl.Rows.AddAt(GridView1.Rows.Count + 1, row);


private TableCell[] CreateCells()

{

 

TableCell[] cells = new TableCell[2];

TableCell cell;

Label lbl;

 

//The order item column

cell = new TableCell();

lbl = new Label();

lbl.Text = txtEmpId.Text;

cell.Controls.Add(lbl);

cells[0] = cell;

 

//The price column

cell = new TableCell();

lbl = new Label();

lbl.Font.Bold = true;

lbl.Text = txtEmpName.Text;

cell.HorizontalAlign = HorizontalAlign.Right;

cell.Controls.Add(lbl);

cells[1] = cell;

 

return cells;

}

Suriya kani replied to karthik karthik on 14-Sep-10 03:17 AM
We can assign the values directly to the gridview cell

ex:
 datagridview1.rows[0].cells[0].value=empname.text;
 datagridview1.rows[0].cells[1].value=empid.text;
karthik karthik replied to Suriya kani on 14-Sep-10 03:20 AM
i should do in the button click event
karthik karthik replied to Sagar P on 14-Sep-10 03:29 AM

Table tbl = (e.Row.Parent as Table);

error in this line in the button click event

Sagar P replied to karthik karthik on 14-Sep-10 03:36 AM
ohh try this one instad of that;

Table tbl = (GridView1.Rows[GridView1.Rows.Count - 1].Parent as Table);

karthik karthik replied to Sagar P on 14-Sep-10 03:47 AM
i am getting this kind of error if i use that

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Sagar P replied to karthik karthik on 14-Sep-10 04:28 AM
Is your gridview has any rows before clicking on this button??

If its empty then you have to create one table and add the cells it in and have to use that table.... instead of this line of code otherwise you can do it.... so try something like this;

try this one instad of that;

 

if (GridView1.Rows.Count <= 0)

{

//Create new table and use it       

DataTable dt = new DataTable();

DataColumn dtCol1 = new DataColumn();

dt.Columns.Add(dtCol1);

dt.Rows.Add(row);

GridView1.DataSource = dt;

GridView1.DataBind();

}

else

{

Table tbl = (GridView1.Rows[GridView1.Rows.Count -1].Parent as Table);

//Add the row at the end of the list, but before the footer.

tbl.Rows.AddAt(GridView1.Rows.Count + 1, row);

}

Anurag replied to karthik karthik on 14-Sep-10 04:36 AM
I think the best way here would be to create 2 more rows for EmpName and EmpID at design time.
Then u can assign values simply by:
gridview1.Rows[i].Cells[j].Text = textboxEmpname.text;
gridview1.Rows[i].Cells[k].Text = textboxEmpId.text;
where "i" is the appropriate row which has to be found out by looping/ matching
"j" is the index of column EmpName and "k" is of EmpID's column index.
karthik karthik replied to Sagar P on 14-Sep-10 05:51 AM
No not working have worked on it.we should write that coding in the button click only a
Sagar P replied to karthik karthik on 14-Sep-10 06:19 AM
Yes you have to write tht code on button click only;

protected void btnadd_Click(object sender, EventArgs e)

{

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

//Add the two Columns 

row.Cells.AddRange(CreateCells());

 

if (GridView1.Rows.Count <= 0)

{

//Create new table and use it   

DataTable dt = new DataTable();

DataColumn dtCol1 = new DataColumn();

dt.Columns.Add(dtCol1);

dt.Rows.Add(row);

GridView1.DataSource = dt;

GridView1.DataBind();

}

else

{

Table tbl = (GridView1.Rows[GridView1.Rows.Count -1].Parent as Table);

//Add the row at the end of the list, but before the footer.

tbl.Rows.AddAt(GridView1.Rows.Count + 1, row);

}
}

Goniey N (Mr. G) replied to karthik karthik on 14-Sep-10 09:00 AM
-- Use Below Code Which Will Add Your Data From The Database To Your DataGridView...

01.private void button1_Click(object sender, EventArgs e)
02.{
03.  dataGridView2.Columns.Add("Col1", "Column1");
04.  dataGridView2.Columns.Add("Col2", "Column2");
05.    System.Collections.ArrayList col1 = new System.Collections.ArrayList();
06.    System.Collections.ArrayList col2 = new System.Collections.ArrayList();
07.    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/mdb1.mdb");
08.    OleDbDataReader dr;
09.    try
10.    {
11.      Conn.Open();
12.        string str = "SELECT Name FROM Student";
13.        OleDbCommand cmd = new OleDbCommand(str, Conn);
14.        dr = cmd.ExecuteReader();
15.        while (dr.Read())
16.        {
17.      //Here It Will Store Names In "col1" Arraylist...
18.          col1.Add(dr.GetValue(0).ToString());
19.        }
20.        Conn.Close();
21.  }
22.    catch (Exception ex)
23.    {
24.        Conn.Close();
25.        MessageBox.Show(ex.ToString());
26.    }
27. 
28.  try
29.    {
30.        Conn.Open();
31.        string str = "SELECT age FROM Student";
32.        OleDbCommand cmd = new OleDbCommand(str, Conn);
33.        dr = cmd.ExecuteReader();
34.        while (dr.Read())
35.        {
36.      //Here It Will Store Ages In "col2" Arraylist...
37.      col2.Add(dr.GetValue(0).ToString());
38.        }
39.        Conn.Close();
40.  }
41.    catch (Exception ex)
42.    {
43.        Conn.Close();
44.        MessageBox.Show(ex.ToString());
45.    }
46.     
47. 
48.  //Here It Will Add Data To The DataGridView...
49.  int i = 0;
50.    while (i < col1.Count)
51.    {
52.    //Add Names...
53.        dataGridView2.Rows[i].Cells[0].Value = col1[i].ToString();
54. 
55.    //Add Ages...
56.        dataGridView2.Rows[i].Cells[1].Value = col2[i].ToString();
57.        i++;
58.  }
59.}


-- For More Clear View See Below Image :





-- It Will Work 100%...

-- Hope This Will Help You...
karthik karthik replied to Goniey N (Mr. G) on 14-Sep-10 09:50 AM
Hai Goniey N

Can U help me as per my requriements.in this u r reterving the data from the database. my requriments is i want to insert the data directly to the gridview without affecting the database.here i should use databind,datareader,dataset,datatable.


tx in advance.
Goniey N (Mr. G) replied to karthik karthik on 14-Sep-10 10:19 PM
-- For That You Have Some Data To Insert Into Grdiview...

-- Here I Assume That You Have One Form & When You Click On Submit Button It Will Show That Data in Your Grdiview Other Wise You Can Add Your Data Also...


01.private void button1_Click(object sender, EventArgs e)
02.{
03.  dataGridView2.Columns.Add("Col1", "UserName");
04.  dataGridView2.Columns.Add("Col2", "ContactNo");
05.  dataGridView2.Columns.Add("Col1", "EmailId");
06. 
07. 
08.  //Add UserNames...
09.  dataGridView2.Rows[0].Cells[0].Value = txtUserName.Text;
10.   
11.  //Add ContactNo...
12.  dataGridView2.Rows[0].Cells[1].Value = txtContactNo.Text;
13.   
14.  //Add EmailId...
15.  dataGridView2.Rows[0].Cells[1].Value = txtEmailId.Text;
16.}


-- It Also Work 100%....

-- Hope This Will Help You....
Suriya kani replied to karthik karthik on 17-Sep-10 05:39 AM
Do you have rows in datagridview before click the button?
if don't have rows u need addrows on gridview and then assign values to the cells
..