Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Extracting data from DataGrid

Hi,

I have the following page:

<script language="C#" runat="server">

void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindDataGrid(); // create data set and bind to grid control
}

void InfoBtn_Click(Object sender, DataGridCommandEventArgs e)
{
//string customerID=MyDataGrid.DataKeys[MyDataGrid.SelectedIndex].ToString();
}

void dgPopularFAQs_Delete(Object sender, DataGridCommandEventArgs objArgs)
{
string strSQL = "delete from Booklist " +
"WHERE ISBN='" + MyDataGrid.DataKeys[objArgs.Item.ItemIndex] + "'";
ExecuteSQLStatement(strSQL);
}


void DoItemEdit(Object objSource, DataGridCommandEventArgs objArgs)
{
lblSQL.Text = ""; // clear text from label that shows SQL statement

// set the EditItemIndex property of the grid to this item's index
MyDataGrid.EditItemIndex = objArgs.Item.ItemIndex;
BindDataGrid(); // bind the data and display it
}

void DoItemUpdate(Object objSource, DataGridCommandEventArgs objArgs)
{
// get a reference to the title and publication date text boxes
TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
TextBox objPubDateCtrl = (TextBox)objArgs.Item.Cells[2].Controls[0];

// create a suitable SQL statement and execute it
string strSQL = "UPDATE Booklist SET Title='" + objTitleCtrl.Text + "', "
+ "PublicationDate='" + objPubDateCtrl.Text + "' "
+ "WHERE ISBN='" + MyDataGrid.DataKeys[objArgs.Item.ItemIndex] + "'";
ExecuteSQLStatement(strSQL);

// set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataGrid.EditItemIndex = -1;
BindDataGrid(); // bind the data and display it
}

void DoItemCancel(Object objSource, DataGridCommandEventArgs objArgs)
{
// set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataGrid.EditItemIndex = -1;
BindDataGrid(); // bind the data and display it
}

void ExecuteSQLStatement(string strSQL)
{
// this is where the SQL statement would be executed against the
// original data source. In this example, we're simply displaying
// the statement in a Label on the page
lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" + strSQL;
}

void BindDataGrid()
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;

// create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610025%'";

// create a variable to hold an instance of a DataReader object
OleDbDataReader objDataReader;

try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);

// open the connection to the database
objConnect.Open();

// create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

// execute the SQL statement against the command to get the DataReader
objDataReader = objCommand.ExecuteReader();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source + "<p />";
return; // and stop execution
}

// set the DataSource property and bind the grid
MyDataGrid.DataSource = objDataReader;
MyDataGrid.DataBind();
}

</script>
</HEAD>
<body bgColor="#ffffff">
<span class="heading">Editing Data in a DataGrid Control</span>
<hr>
<!----------------------->
<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"></wrox:connect>
<div id="outError" runat="server"></div>
<ASP:LABEL id="lblSQL" runat="server"></ASP:LABEL>
<p>
<form id="Form1" runat="server">
<ASP:DATAGRID id="MyDataGrid" runat="server" AutoGenerateColumns="False" OnCancelCommand="DoItemCancel"
OnUpdateCommand="DoItemUpdate" OnEditCommand="DoItemEdit" DataKeyField="ISBN" EditItemStyle-BackColor="yellow"
CellPadding="2" OnDeleteCommand="dgPopularFAQs_Delete" OnItemCommand="InfoBtn_Click">
<Columns>
<ASP:TemplateColumn HeaderText="Title">
<ItemTemplate>
<asp:LinkButton ID="TestLnkBtn" CommandName="select" Runat="server">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<ASP:TextBox id="txtTitle" Size="60" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
</EditItemTemplate>
</ASP:TemplateColumn>
<ASP:BoundColumn DataField="PublicationDate" HeaderText="Published" />
<ASP:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
<asp:ButtonColumn Text="Delete" CommandName="Delete" />
</Columns>
</ASP:DATAGRID></form>

I want to be able to clink one of the linkbuttons for Title and be redirected to a new page with the value of the Title field attached to the query string.

I don't know how to extract the data since EventArgs doesn't have the property Item such as in DataGridCommandEventArgs. I also tried MyDataGrid.DataKey[MyDataGrid.SelectedIndex].ToString(), but SelectedIndex has not been initialized. Please help. I have also attached a printscreen of the output for this page.
[6350 byte] By [mmingfeilam] at [2007-11-11 8:14:06]
# 1 Re: Extracting data from DataGrid
Dear mmingfeilam,

The only problem in your code is you have used MyDataGrid.SelectedIndex instead of e.Item.ItemIndex which actually refer to current row number, so to fix this problem change your code for InfoBtn_Click with following code:

void InfoBtn_Click(Object sender, DataGridCommandEventArgs e)
{
string customerID=MyDataGrid.DataKeys[e.Item.ItemIndex].ToString();
}
egurdarshan at 2007-11-11 23:13:23 >