Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox in Datagrid 1

Status
Not open for further replies.

stfarm

Programmer
May 31, 2001
179
CA
Hi,

I am populating a datagrid with data from a strored proc. I would like to have a textbox next to each record where the use can enter comments. My DBA gave me a stored proc to put the data into the database.

I was wondering how I could do that. Any help is apprciated. I am fairly new to ASP.NET

Thank You,
Steve
 
hook the text box for your comments into an event processor:
add to the InitializeComponent() section an entry for your textbox name like:
textBox.TextChanged+= new System.EventHandler(this.textBox_Changed);
then put into the event process handler (if you have Vs hit two tabs to create it) textBox_Changed() the code for inserting the comment.
 
You can just use a TemplateColumn in the grid with a TextBox in it. If it's a mass update, then all you need is a button on click of which you'll loop through the grid, extract Text property of a text box and do the update.
 
You guys rule. I will try that right now. Thank You.

Steve
 
Ok, I got the textbox in. Now I have a datagrid that I need to process somehow.

I need to use the data from the datagrid, and pass it to a stored proc.

I was thinking to create a DataReader when I click a button, then loop thru the data, and execute my Proc.

How can I get the Data from the DataGrid into a datareader??

Thank you,
Steve
 
You don't need the DataReader, since you're not retrieving records from the database but updating them. You need to loop through the grid, retrieve value of a TextBox in each row and update the db. Assumming that you store the unique identifyer of a database row to update in the DataKeyField property of the grid:
Code:
<asp:DataGrid ID="myGrid" runat=server DataKeyField="keyFieldName">
  <asp:TemplateColumn>
    <ItemTemplate>
      <asp:TextBox ID="myText" runat=server />
    </ItemTemplate>
  </asp:TemplateColumn>
.............
</asp:DataGrid>
<asp:Button ID="btnUpdate" Text="Update" runat=server />

code behind:
private void btnUpdate_Click(object sender, System.EventArgs e)
{
  foreach(DataGridItem dgItem in myGrid.Items)
  {
    // retrieve text    
    TextBox txt = (TextBox)dgItem.FindControl("myText");
    string s = txt.Text;

    // assumming that the unique key is integer
    int key = (int)myGrid.DataKeys[dgItem.ItemIndex];

    // put here the logic to update the db record
    SqlConnection conn = new SqlConnection(connectionString);
    string cmdText = "update sometable set somefield='" + s + "' where primKey=" + key.ToString();
    SqlCommand cmd = new SqlCommand(cmdText, conn);
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();
  }
}
Something like this. You can look at the tutorials at look for the "Server-Side Data Access" link.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top